我正在玩這個導航欄,並且無論何時您將鼠標懸停在其上方,「關於和投資組合」菜單都會不斷向前延伸。 我想阻止它擴展。 您可以從JSFiddle檢查代碼。提前致謝。如何阻止主菜單在光標懸停時延長
https://jsfiddle.net/JustCraze/gdc56899/1/#
HTML
<ul id="main-nav">
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About ↓</a>
<ul class="hidden">
<li>
<a href="#">Who We Are</a>
</li>
<li>
<a href="#">What We Do</a>
</li>
</ul>
</li>
<li>
<a href="#">Portfolio ↓</a>
<ul class="hidden">
<li>
<a href="#">Photography</a>
</li>
<li>
<a href="#">Web & User Interface Design</a>
</li>
<li>
<a href="#">Illustration</a>
</li>
</ul>
</li>
<li>
<a href="#">News</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
CSS
#wrap-nav {
display: block;
margin:2rem;
z-index: 900;
padding: 5rem;
}
#main-nav {
display: block;
z-index: 950;
padding: 1rem;
float: right;
}
/*Strip the ul of padding and list styling*/
ul {
list-style-type:none;
margin:0;
padding:.1rem;
}
/*Create a horizontal list with spacing*/
li {
display:inline-block;
float: left;
margin-right: 1px;
}
/*Style for menu links*/
li a {
display:block;
min-width:140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #fff;
background: #2f3036;
text-decoration: none;
}
/*Hover state for top level links*/
li:hover a {
background: #19c589;
z-index: 980;
}
/*Style for dropdown links*/
li:hover ul a {
background: #f3f3f3;
color: #2f3036;
height: 40px;
line-height: 40px;
z-index: 980;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
background: #19c589;
color: #fff;
z-index: 980;
}
/*Hide dropdown links until they are needed*/
li ul {
display: none;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
float: none;
}
/*Prevent text wrapping*/
li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
display: block;
}
/*Responsive Styles*/
@media screen and (max-width : 250px){
/*Make dropdown links appear inline*/
ul {
position: static;
display: none;
}
/*Create vertical spacing*/
li {
margin-bottom: 1px;
}
/*Make all menu links full width*/
ul li, li a {
width: 100%;
}
如果您正在鏈接到JSfiddle,請不要將URL設置爲格式化,就好像它是爲了代碼塊一樣。內聯代碼滴答也是不必要的。 – Boysenb3rry
我的道歉,仍然試圖獲得stackoverflow的掛鉤。這個怎麼樣? – Bobby
好多了,謝謝。我希望下面的回答有幫助,或者讓你更接近你正在尋找的答案。 – Boysenb3rry