每当我将鼠标悬停在具有下拉菜单的按钮下时,都会显示下拉菜单。我想这样做,所以当我将鼠标放在实际按钮上时,只显示下拉菜单。
HTML
<div id="navbar">
<ul>
<li><a href="../HTML/home.html">Home</a></li>
<li><a href="../HTML/cakes/cakes.html">Cakes</a>
<ul>
<li ><a href="#">Budget Cakes</a></li>
<li><a href="#">Wedding</a></li>
<li ><a href="#">Cakes to Go</a></li>
<li ><a href="#">Cake Bonbons</a></li>
<li ><a href="#">Holiday Cakes</a></li>
<li ><a href="#">Cakes for Girls</a></li>
<li ><a href="#">Cakes for Boys</a></li>
<li ><a href="#">For Her</a></li>
<li ><a href="#">For Him</a></li>
<li ><a href="#">Cupcakes</a></li>
</ul>
</li>
<li><a href="#">Classes</a></li>
<li><a href="#">Flavors</a></li>
<li><a href="#">Events</a>
<ul>
<li><a href="#">Beach Wedding</a></li>
<li><a href="#">Baby Showers</a></li>
<li><a href="#">Sweet 15-16</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
<li><a href="#">Questions</a></li>
</ul>
</div><!--end of navbar-->
CSS
#navbar ul li:hover > ul {
height:auto !important;
opacity:1;
}
#navbar ul li:hover > ul > li {
height:50px !important;
opacity:1;
}
#navbar ul {
background: #efefef;
background: linear-gradient(top, #efefef 0%, #bbbbbb 100%);
background: moz-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
background: webkit-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.15);
padding: 0 40px;
border-radius:10px;
-moz-border-radius:10px;
-webkit-border-radius:10px;
list-style:none;
position:relative;
display:inline-table;
}
#navbar ul:after {
content:"";
clear: both;
display: block;
}
#navbar ul li {
float:left;
-webkit-transition:background 0.3s ease-in;
-moz-transition:background 0.3s ease-in;
-o-transition:background 0.3s ease-in;
-ms-transition:background 0.3s ease-in;
transition:background 0.3s ease-in;
position:relative;
}
#navbar ul li:hover {
background:#4b545f;
}
#navbar ul li:hover a {
color:#fff;
}
#navbar ul li a {
display:block;
padding:25px 40px;
color:#757575;
text-decoration:none;
}
#navbar ul ul {
background: #5f6975;
border-radius:0px;
-webkit-border-radius:0px;
-moz-border-radius:0px;
padding: 0;
position:absolute;
top:100%;
left:0px;
width:195px;
z-index:1;
-webkit-transition:all 0.4s ease-in-out;
transition:all 0.4s ease-in-out;
opacity:0;
height:0px;
}
#navbar ul ul li {
float:none;
border-top:1px solid #6b727c;
border-bottom: 1px solid #575f6a;
position:relative;
height:0px;
-webkit-transition:all 0.4s ease-in-out;
transition:all 0.4s ease-in-out;
opacity:0;
}
#navbar ul ul li a {
padding: 15px 40px;
color:#fff;
}
#navbar ul ul li a:hover {
background: #4b545f;
}
#navbar {
text-align:center;
}
#navbar ul ul ul {
position:absolute;
left:100%;
top:0;
width:155px;
}
答案 0 :(得分:1)
这是因为您的嵌套<ul>
仍有高度,因此当您将鼠标悬停在其上时,浏览器会将其视为悬停在第一个<li>
的一部分上。
最简单的修复方法是将subnav ul设置为display: block
和overflow: hidden
。
#navbar ul ul {
...(existing styles)
display: block;
overflow: hidden;
}
<强> Demo 强>
或者,您可以切换visibility: hidden
。
#navbar ul ul {
...(existing styles)
visibility: hidden; /* ADD */
}
#navbar ul li:hover > ul {
...(existing styles)
visibility: visible; /* ADD */
}
<强> Demo 2 强>
答案 1 :(得分:0)
添加以下CSS。
#navbar ul ul {
display: block;
overflow: hidden;
}