单击其他子菜单项时关闭子菜单

时间:2015-05-20 19:04:40

标签: jquery

你好我有这个代码来控制菜单,它工作得很好,但我也想要当我点击打开其他子菜单时关闭所有其他子菜单。 简而言之:点击一个+打开一个子菜单,点击另一个+打开这个子菜单并关闭第一个子菜单。

谢谢。

看起来我必须添加更多细节,但我不知道还能说些什么。

/*
Flaunt.js v1.0.0
by Todd Motto: http://www.toddmotto.com
Latest version: https://github.com/toddmotto/flaunt-js

Copyright 2013 Todd Motto
Licensed under the MIT license
http://www.opensource.org/licenses/mit-license.php

Flaunt JS, stylish responsive navigations with nested click to reveal.
*/
;(function($) {

// DOM ready
$(function() {

// Add some classes and Append the mobile icon nav
$('.nav').append($('<div class="nav-mobile"></div>'));
$('.nav > ul').addClass('nav-list');
$('.nav > ul > li').addClass('nav-item');
$('.nav > ul > li > ul').addClass('nav-submenu');
$('.nav > ul > li > ul > li').addClass('nav-submenu-item');

// Add a <span> to every .nav-item that has a <ul> inside. And add an sub menu icon indicator.
$('.nav-item').has('ul').prepend('<span class="nav-click"><i></i></span>');

// Click to reveal the mobile menu
$('.nav-mobile').click(function(){
$('.nav-list').toggle();
$('.nav-submenu').hide(); // This will close the submenu when i click the top ribbon (.nav-mobile) to close the mobile menu
if(!$('.nav-list').is(':visible')){ // the menu was closed because it's not visible anymore
$('.nav-item .nav-click').each(function(){ // loop through nav clicks
if($(this).hasClass('icon-close')) { // This will toggle back the + icon on mobile menu close/open
$(this).toggleClass('icon-close');           
}
}); 
}
});

// Dynamic binding to on 'click' and Toggle the nested nav
$('.nav-list').on('click', '.nav-click', function(){
$(this).siblings('.nav-submenu').toggle();

// This will toggle the + and - when clicked
$(this).removeClass('nav-click');
$(this).toggleClass('icon-close');
$(this).toggleClass('nav-click');   
});

// This will toggle the menu/submenu/- when click outside of the menu
$('.wrapper').click(function(event) {
$('html').one('click',function() {
$('.nav-list').hide();
$('.nav-submenu').hide(); // This will close the submenu when you click the top ribbon (hamburger button) to close the mobile menu
if(!$('.nav-list').is(':visible')){ // the menu was closed because it's not visible anymore
$('.nav-item .nav-click').each(function(){ // loop through nav clicks
if($(this).hasClass('icon-close')) { // This will toggle the +/- icon on mobile menu close/open
$(this).toggleClass('icon-close');
}
}); 
}
});
event.stopPropagation();
});

}); 

})(jQuery);

Live demo jsFiddle

1 个答案:

答案 0 :(得分:2)

可能有更好的方法,但我认为它可以按照你想要的方式工作。

http://jsfiddle.net/L73dqxhd/

从第40行开始,我将其更改为:

// Dynamic binding to on 'click' and Toggle the nested nav
$('.nav-list').on('click', '.nav-click', function(){
    var currentSubmenu = $(this).siblings('.nav-submenu');
    var currentNavClick = $(this);
    $('.nav-submenu').not(currentSubmenu).slideUp();
    $('.nav-click').not(currentNavClick).removeClass('icon-close');
    $(this).siblings('.nav-submenu').toggle();

FWIW我真的建议使用bootstrap这样的东西,而不是滚动你自己。

它涵盖了各种各样的基础,并使各种各样的东西,像这样,更容易......