I've coded a navigation menu which has nested dropdowns within - I've coded the menus to slide down on hover and that's working fine.
However there is also a 'show all' link which when clicked expands all the sub-menus and applies a new class to the container to pad the menus out.
Clicking expands the menu fine but then clicking a second time alters the appearance as it should for a split second by removing the class as if its triggering the function again. My code is below - any help would be greatly appreciated! Andy
// == MENU SHOW ALL TOGGLE
// show pointer
$('#menu li#show-all').hover(function(){
$(this).css({'cursor' : 'pointer'});
});
$('#menu-container').addClass('show-single');
function toggleheight(){
if ($('#menu-container').hasClass('show-single')) {
$('#menu li').not('#show-all').fadeOut(function(){
$('#menu').animate({
height : '300px'
}, function(){
$('#menu-container').removeClass('show-single').addClass('show-all');
开发者_如何学Python $('#menu li').not('#show-all').fadeIn();
});
});
}else {
$('#menu li').not('#show-all').fadeOut(function(){
$('#menu-container').removeClass('show-all').addClass('show-single');
$('#menu').animate({
height : '16px'
}, function(){
$('#menu li').not('#show-all').fadeIn();
});
});
}
}
$('#menu li#show-all').click(function(){
$(toggleheight);
});
EDIT : expanding the menu, hovering a link then moving back and clicking the show all link does revert the menu as it should - however clicking the 'show all' link once to expand the once more to shrink without navigating away seems to re-trigger the functions
I decided not to dig into your code and try to fix it because I think there's a much better approach to building navigation menus, with a simple jQuery plugin. However, this doesn't really help you solve your problem with your code, but hopefully you'll find this useful anyways.
So, here's what I came up with.
HTML:
<ul class="nav-menu">
<li>
<a href="#">Menu 1</a>
<ul>
<li><a href="#">Submenu 1</a></li>
<li><a href="#">Submenu 2</a>
<ul>
<li><a href="#">Subsubmenu 1</a></li>
<li><a href="#">Subsubmenu 2</a></li>
<li><a href="#">Subsubmenu 3</a></li>
<li><a href="#">Subsubmenu 4</a></li>
</ul>
</li>
<li><a href="#">Submenu 3</a></li>
<li><a href="#">Submenu 4</a></li>
</ul>
</li>
<li>
<a href="#">Menu 2</a>
<ul>
<li><a href="#">Submenu 1</a></li>
<li><a href="#">Submenu 2</a></li>
<li><a href="#">Submenu 3</a></li>
<li><a href="#">Submenu 4</a></li>
</ul>
</li>
</ul>
Plugin code:
(function($){
function traverseItems($items) {
$items.each(function(){
var $item = $(this),
$submenu = $item.children("ul").hide();
if ($submenu.length) {
$item.children("a").click(function(){
$submenu.toggle();
return false;
}).append(" \>");
traverseItems($submenu.children("li"));
}
});
}
$.fn.navMenu = function (options){
var defaults = {
showAllToggle: true
};
options = $.extend({}, defaults, options);
return this.each(function(){
var $this = $(this)
.addClass("nav-menu"),
$items = $this.children("li");
traverseItems($items);
if (options.showAllToggle) {
$showAll = $("<a></a>")
.attr("href", "#")
.addClass("show-all")
.text("Toggle all")
.toggle(function(){
$this.find("ul").show();
return false;
}, function(){
$this.find("ul").hide();
return false;
})
.wrap("<li></li>")
.parent()
.prependTo($this);
}
});
};
})(jQuery);
Usage:
$(function(){
$(".nav-menu").navMenu();
});
You can see it all in action on jsFiddle.
I can elaborate on this answer and cut it down into pieces if requested. If anyone have any suggestions or ideas on how this can be improved, I'm all ears.
精彩评论