我有HTML菜单和子菜单。出于某种原因,当我在它们上空盘旋时,子菜单会隐藏。当我将鼠标悬停在父菜单上时,子菜单会显示,并且只要将鼠标悬停在子菜单上,它就会隐藏。
HTML
<div id="main-menu">
<ul>
<li><a href="#" class="sf-with-ul">Home<span class="sf-sub-indicator"><i class="icon-angle-down"></i></span><span class="sub">Start here</span></a></li>
<li><a href="#" class="sf-with-ul">Features<span class="sf-sub-indicator"><i class="icon-angle-down"></i></span><span class="sub">remix all features</span></a>
<ul class="transparency">
<li><a href="#">Accordion</a></li>
<li><a href="#">Buttons</a></li>
<li><a href="#" class="sf-with-ul">Typography</a></li>
</ul>
</li>
<li><a href="#">MP3<span class="sub">full archive</span></a></li>
<li><a href="#">Video<span class="sub">latest clips</span></a></li>
<li><a href="#" class="sf-with-ul">Gallery<span class="sf-sub-indicator"><i class="icon-angle-down"></i></span><span class="sub">Photo Gallery</span></a>
<ul class="transparency">
<li><a href="#">Gallery 4 Column</a></li>
<li><a href="#">Gallery 3 Column</a></li>
<li><a href="#">Gallery 2 Column</a></li>
</ul>
</li>
<li><a href="#">Pages<span class="sf-sub-indicator"><i class="icon-angle-down"></i></span><span class="sub">more templates</span></a>
<ul class="transparency">
<li><a href="#">RTL Support</a></li>
<li><a href="#">Events</a></li>
<li><a href="#">MP3 Single Wide</a></li>
</ul>
</li>
<li><a href="#">Blog<span class="sf-sub-indicator"><i class="icon-angle-down"></i></span><span class="sub">latest news</span></a>
<ul class="transparency">
<li><a href="#">Blog</a></li>
<li><a href="#">Blog Left Sidebar</a></li>
<li><a href="#">Blog Both Sidebar</a></li>
</ul>
</li>
</ul>
</div>
CSS
#main-menu{
float: right;
position: relative;
margin: 20px 0 0 -20px;
}
#main-menu ul{
float: right;
padding: 0;
position: relative;
margin: 0;
list-style: none;
}
#main-menu li{
float: left;
position: relative;
padding: 0;
}
#main-menu a{
padding:0 20px 22px 0;
display: block;
position: relative;
color: #fff;
text-transform: uppercase;
font-size: 14px;
text-decoration: none;
font-weight: bold;
}
#main-menu li a span {
font-size: 12px;
font-family: tahoma;
font-weight: normal;
text-transform: none;
display: block;
color: #AFAFAF;
margin: 10px 0 0 0;
}
#main-menu li ul{
display: none;
position: absolute;
width: 170px;
word-break: break-all;
}
#main-menu li:hover > ul{
display: block;
margin-top: 11px;
}
#main-menu li > ul li{
display: block;
width: 100%;
}
#main-menu li > ul li a:hover{
display: block;
width: 100%;
background: #FFF;
color: #000;
}
#main-menu li > ul li a{
font-weight: normal;
padding: 8px 15px;
line-height: 20px;
color: #fff;
text-transform: none;
letter-spacing: 0;
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
答案 0 :(得分:2)
<强> Live demo 强>
只需在此CSS中替换margin-top
padding-top
:
#main-menu li:hover > ul {
display: block;
margin-top: 11px;
}
这样就变成了:
#main-menu li:hover > ul {
display: block;
padding-top: 11px;
}
<强>证明强>:
答案 1 :(得分:1)
你可以更新
#main-menu li ul{
display: none;
position: absolute;
left:0;
top:0;
width: 170px;
word-break: break-all;
}
和
#main-menu li > ul li a{
font-weight: normal;
padding: 8px 15px;
line-height: 20px;
color: #000;
text-transform: none;
letter-spacing: 0;
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
进一步对齐u cand根据需要更新
答案 2 :(得分:1)
将margin-top
更改为padding-top
,如下所示:
#main-menu li:hover > ul {
display: block;
padding-top: 11px;
}