菜单与其他菜单重叠。当您单击第一个菜单“关于我们”时,它会打开。但是当您点击“产品”时,“关于我们”不会关闭,因此“产品”菜单与“关于我们”菜单重叠。
HTML
<body>
<nav>
<ul class="nav">
<li><a href="#"></a></li>
<li class="menuabout"><a>About</a>
<ul class="subnav01 clearfix">
<li><a href="#">About One</a></li>
<li><a href="#">About Two</a></li>
<li><a href="#">About Three</a></li>
<li><a href="#">About Four</a></li>
<li><a href="#">About Five</a></li>
<li><a href="#">About Six</a></li>
</ul>
</li>
<li class="menuproducts"><a>Products</a>
<ul class="subnav02 clearfix">
<li><a href="#">Product One</a></li>
<li><a href="#">Product Two</a></li>
<li><a href="#">Product Three</a></li>
<li><a href="#">Product Four</a></li>
<li><a href="#">Product Five</a></li>
<li><a href="#">Product Six</a></li>
</ul>
</li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
CSS
* {
margin: 0;
padding: 0;
}
body {
width: 100%;
height: 100%;
background: darkgray;
}
.nav {
width: 100px;
position: absolute;
top: 30px;
right: 20px;
list-style: none;
}
.nav li {
margin: 0 0 10px 0;
font-family: Helvetica;
font-size: 11px;
text-align: right;
text-transform: uppercase;
line-height: 1;
}
.nav li:nth-child(5) {
margin: 0;
}
.nav li a {
height: 10px;
padding: 10px 10px 10px 0;
color: white;
text-decoration: none;
display: block;
cursor: pointer;
position: relative;
}
.subnav01, .subnav02 {
width: 300px;
position: absolute;
top: 29px;
left: -300px;
display: none;
}
.subnav01 li, .subnav02 li {
width: 150px;
margin: 0;
text-align: center;
float: left;
}
.subnav01 li a, .subnav02 li a {
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
background: dodgerblue;
height: 10px;
display:block;
}
.subnav01 li a:hover, .subnav02 li a:hover {
background: dimgray;
}
.clearfix:before,
.clearfix:after {
content: " "; /* 1 */
display: table; /* 2 */
}
.clearfix:after {
clear: both;
}
.active > a {
background: dodgerblue !important;
}
JS / jQuery / jQuery UI /
jQuery(document).ready(function($){
$(".menuabout").on("click", function(){
$(".subnav01").toggle ("slide", {direction: "right"}, "slow");
$(this).toggleClass("active");
});
$(".menuproducts").on("click", function(){
$(".subnav02").toggle ("slide", {direction: "right"}, "slow");
$(this).toggleClass("active");
});
});
答案 0 :(得分:2)
menu
,因此您可以为所有菜单元素使用一个函数
jQuery(document).ready(function($){
$(".menu").on("click", function(){
deactivateAll(this);
toggle(this);
});
function deactivateAll(current) {
$( "li" ).each(function( index, elem ){
if (elem != current && $(elem).hasClass("active")) {
toggle(elem);
}
});
}
function toggle(elem) {
$(elem).find("ul").toggle ("slide", {direction: "right"}, "slow");
$(elem).toggleClass("active");
}
});
答案 1 :(得分:1)
这有效:(Fiddle)
jQuery(document).ready(function($){
$(".menuabout").on("click", function(){
reset();
$(".subnav01").toggle ("slide", {direction: "right"}, "slow");
$(this).toggleClass("active");
});
$(".menuproducts").on("click", function(){
reset();
$(".subnav02").toggle ("slide", {direction: "right"}, "slow");
$(this).toggleClass("active");
});
function reset() {
$("ul.nav li").removeClass("active");
$("ul.nav li ul").hide();
}
});
你也可以稍微重构你的代码,这样它就不会那么重复了。添加更多根导航元素意味着每次都复制代码:)