我正在建立一种下拉菜单,显然我希望我的子菜单项保持隐藏状态,直到用户将鼠标悬停在适当的元素上并使它们显示出来。我的问题是我得到了这种讨厌的淡入淡出效果,我无法让它工作,谁能帮我解决这个问题?你可以在这里找到我的代码。
HTML 代码:
<nav id="mainNav">
<ul>
<li class="topMenu"><a href="#">Section #1</a></li>
<li class="topMenu"><a href="#">Section #2</a></li>
<li class="topMenu"><a href="#">Section #3</a>
<ul>
<li><a href="#">SubMenu Item #1</a></li>
<li><a href="#">SubMenu Item #2</a></li>
<li><a href="#">SubMenu Item #3</a></li>
</ul>
</li>
<li class="topMenu"><a href="#">Section #4</a>
<ul>
<li><a href="#">SubMenu Item #1</a></li>
<li><a href="#">SubMenu Item #2</a></li>
<li><a href="#">SubMenu Item #3</a></li>
<li><a href="#">SubMenu Item #4</a></li>
<li><a href="#">SubMenu Item #5</a></li>
</ul>
</li>
<li class="topMenu"><a href="#">Section #5</a>
<ul>
<li><a href="#">SubMenu Item #1</a></li>
<li><a href="#">SubMenu Item #2</a></li>
</ul>
</li>
<li class="topMenu"><a href="#">Section #6</a></li>
</ul>
</nav>
</p>
CSS
body {
width: 960px;
margin: 0 auto;
}
a {
text-decoration: none;
}
div#menu {
width: 960px;
height: 50px;
background-color: #000;
border-bottom: 2px solid #ff0078;
}
div#menu nav#mainNav a {
font-family: 'BebasNeueRegular', 'Arial, Helvetica, sans-serif';
color: #e4e4e4;
text-transform: uppercase;
font-size: 20px;
}
div#menu nav#mainNav ul li {
top: 17px;
left: 25px;
margin-right: 30px;
display: inline;
position: relative;
}
div#menu nav#mainNav ul li ul {
display: none;
position: absolute;
top: 25px;
width: 150px;
}
div#menu nav#mainNav ul li ul li {
display: block;
left: 0;
margin-bottom: 5px;
padding: 5px;
background: #000;
}
div#menu nav#mainNav ul li ul li:hover {
border-bottom: 1px solid #ff0078;
}
div#menu nav#mainNav ul li ul li a {
display: block;
}
JS 脚本
$(document).ready(function() {
var menuItems = $('div#menu nav#mainNav ul').find('li.topMenu');
menuItems.on('mouseover', function() {
var submenu = $(this).find('ul');
$(this).find('ul').fadeIn();
});
menuItems.on('mouseout', function() {
var submenu = $(this).find('ul');
$(this).find('ul').fadeOut();
});
});