当第一个菜单切换时二级菜单消失?
Secondary menu disappear when first menu toggle?
我的问题:我有一个大型菜单,当切换项目时我希望我的二级菜单消失
$(document).on('click', '.yamm .dropdown-menu', function (e) {
e.stopPropagation()
})
var toggleButton = document.getElementById('toggleSlider'),
slide = document.querySelector('.slide');
toggleButton.addEventListener('click', toggleSlider, false);
function toggleSlider() {
if (slide.classList.contains('slide-up')) {
slide.classList.remove('slide-up');
} else {
slide.classList.add('slide-up');
}
}
function show(target) {
document.getElementById(target).style.display = 'block';
}
function hide(target) {
document.getElementById(target).style.display = 'none';
}
function showhide(id) {
var e = document.getElementById(id);
if (e.style.display == 'none') {
e.style.display = 'block';
} else {
e.style.display = 'none';
}
}
我以为这样就可以了..
只需要添加这个 jquery 部分。
$("button").on("click", function (e) {
console.log("dg");
if ($("button").hasClass('cl')) {
$("button").removeClass("cl");
$('#sous-menu')..fadeIn();
} else {
$("button").addClass("cl");
$('#sous-menu').fadeOut();
}
});
$('.dropdown-toggle').dropdown();
只需监听 bootstraps 下拉事件,然后显示和隐藏您的 sous-menu。看一下代码段。
$(document).on('click', '.yamm .dropdown-menu', function(e) {e.stopPropagation()})
$('.dropdown').on('show.bs.dropdown', function () {
$('#sous-menu').hide();
})
$('.dropdown').on('hide.bs.dropdown', function () {
$('#sous-menu').show();
})
/*!
* Yamm!3 - Yet another megamenu for Bootstrap 3
* http://geedmo.github.com/yamm3
*
* @geedmo - Licensed under the MIT license
*/
.yamm .nav,
.yamm .collapse,
.yamm .dropup,
.yamm .dropdown {
position: static; padding-left:0!important; padding-right:0!important
}
.yamm .container {
position: relative;
}
.yamm .dropdown-menu {
left: auto;
}
.yamm .yamm-content {
padding: 20px 30px;
}
.yamm .dropdown.yamm-fw .dropdown-menu {
left: 0;
right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="col-md-9">
<div class="navbar navbar-default yamm fw">
<div class="navbar-header fw">
<button type="button" data-toggle="collapse" data-target="#navbar-collapse-grid" class="navbar-toggle"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
</div>
<div id="navbar-collapse-grid" class="navbar-collapse collapse navbar-right fw">
<ul class="nav navbar-nav fw">
<!-- DECOUVRIR LA SEM -->
<li class="dropdown yamm-fw col-md-4"><a href="#" data-toggle="dropdown" class="dropdown-toggle">discover</a>
<ul class="dropdown-menu bggreen">
<li class="grid-menu">
<div class="col-md-4">best practices</div>
<div class="col-md-4">team</div>
<div class="col-md-4">partners</div>
</li>
</ul>
</li>
<li class="dropdown yamm-fw col-md-4"><a href="#" data-toggle="dropdown" class="dropdown-toggle">services</a>
<ul class="dropdown-menu bgyellow">
<li class="grid-menu">
<div class="col-md-4">item 1</div>
<div class="col-md-4">item 2</div>
<div class="col-md-4">item 3</div>
</li>
</ul>
</li>
<li class="dropdown yamm-fw col-md-4"><a href="#" data-toggle="dropdown" class="dropdown-toggle">work with us</a>
<ul class="dropdown-menu bgblue">
<li class="grid-menu">
<div class="col-md-4">item 1</div>
<div class="col-md-4">item 2</div>
<div class="col-md-4">item 3</div>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="col-md-9" id="sous-menu">
<div class="col-md-2" style="background:white">item1</div>
<div class="col-md-3" style="background:white">item2</div>
<div class="col-md-3" style="background:white">item3</div>
<div class="col-md-4" style="background:white">item4</div>
</div>
</div>
我确实从您的 li 元素中删除了对 showhide 函数的内联调用。你不需要它们。
我的问题:我有一个大型菜单,当切换项目时我希望我的二级菜单消失
$(document).on('click', '.yamm .dropdown-menu', function (e) {
e.stopPropagation()
})
var toggleButton = document.getElementById('toggleSlider'),
slide = document.querySelector('.slide');
toggleButton.addEventListener('click', toggleSlider, false);
function toggleSlider() {
if (slide.classList.contains('slide-up')) {
slide.classList.remove('slide-up');
} else {
slide.classList.add('slide-up');
}
}
function show(target) {
document.getElementById(target).style.display = 'block';
}
function hide(target) {
document.getElementById(target).style.display = 'none';
}
function showhide(id) {
var e = document.getElementById(id);
if (e.style.display == 'none') {
e.style.display = 'block';
} else {
e.style.display = 'none';
}
}
我以为这样就可以了.. 只需要添加这个 jquery 部分。
$("button").on("click", function (e) {
console.log("dg");
if ($("button").hasClass('cl')) {
$("button").removeClass("cl");
$('#sous-menu')..fadeIn();
} else {
$("button").addClass("cl");
$('#sous-menu').fadeOut();
}
});
$('.dropdown-toggle').dropdown();
只需监听 bootstraps 下拉事件,然后显示和隐藏您的 sous-menu。看一下代码段。
$(document).on('click', '.yamm .dropdown-menu', function(e) {e.stopPropagation()})
$('.dropdown').on('show.bs.dropdown', function () {
$('#sous-menu').hide();
})
$('.dropdown').on('hide.bs.dropdown', function () {
$('#sous-menu').show();
})
/*!
* Yamm!3 - Yet another megamenu for Bootstrap 3
* http://geedmo.github.com/yamm3
*
* @geedmo - Licensed under the MIT license
*/
.yamm .nav,
.yamm .collapse,
.yamm .dropup,
.yamm .dropdown {
position: static; padding-left:0!important; padding-right:0!important
}
.yamm .container {
position: relative;
}
.yamm .dropdown-menu {
left: auto;
}
.yamm .yamm-content {
padding: 20px 30px;
}
.yamm .dropdown.yamm-fw .dropdown-menu {
left: 0;
right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="col-md-9">
<div class="navbar navbar-default yamm fw">
<div class="navbar-header fw">
<button type="button" data-toggle="collapse" data-target="#navbar-collapse-grid" class="navbar-toggle"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
</div>
<div id="navbar-collapse-grid" class="navbar-collapse collapse navbar-right fw">
<ul class="nav navbar-nav fw">
<!-- DECOUVRIR LA SEM -->
<li class="dropdown yamm-fw col-md-4"><a href="#" data-toggle="dropdown" class="dropdown-toggle">discover</a>
<ul class="dropdown-menu bggreen">
<li class="grid-menu">
<div class="col-md-4">best practices</div>
<div class="col-md-4">team</div>
<div class="col-md-4">partners</div>
</li>
</ul>
</li>
<li class="dropdown yamm-fw col-md-4"><a href="#" data-toggle="dropdown" class="dropdown-toggle">services</a>
<ul class="dropdown-menu bgyellow">
<li class="grid-menu">
<div class="col-md-4">item 1</div>
<div class="col-md-4">item 2</div>
<div class="col-md-4">item 3</div>
</li>
</ul>
</li>
<li class="dropdown yamm-fw col-md-4"><a href="#" data-toggle="dropdown" class="dropdown-toggle">work with us</a>
<ul class="dropdown-menu bgblue">
<li class="grid-menu">
<div class="col-md-4">item 1</div>
<div class="col-md-4">item 2</div>
<div class="col-md-4">item 3</div>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="col-md-9" id="sous-menu">
<div class="col-md-2" style="background:white">item1</div>
<div class="col-md-3" style="background:white">item2</div>
<div class="col-md-3" style="background:white">item3</div>
<div class="col-md-4" style="background:white">item4</div>
</div>
</div>
我确实从您的 li 元素中删除了对 showhide 函数的内联调用。你不需要它们。