[英]How to make accordion menu close and open sequentially rather than simultaneously?
I've got a jQuery plug-in to generate accordion-like menus. 我有一个jQuery插件来生成类似手风琴的菜单。 However, my slideUp()
and slideDown()
functions seem to be happening simultaneously. 但是,我的slideUp()
和slideDown()
函数似乎同时发生。
Ideally, I would like all expanded sub-menus to slide up, and then the selected menu item's sub-menu slide down once that's completed. 理想情况下,我希望所有展开的子菜单向上滑动,然后在完成后将所选菜单项的子菜单向下滑动。 My code is as follows: 我的代码如下:
jQuery.fn.accordionMenu = function() {
return this.each(function() {
$('#menu ul').hide(); // hide all unordered lists
$('#menu li.selected ul').show(); // drop down selected item's sub-menu
$('#menu li a').click(function() {
var speed = 'fast';
var checkElement = $(this).next();
if (checkElement.is('ul')) {
if (!checkElement.is(':visible')) {
$('#menu ul:visible').slideUp('slow').parent().removeClass('open');
checkElement.slideDown('slow').parent().addClass('open');
}
return false;
}
});
});
};
Thanks in advance. 提前致谢。
EDIT: Solved with the following: 编辑:解决以下问题:
jQuery.fn.accordionMenu = function() {
return this.each(function() {
$('#menu ul').hide(); // hide all unordered lists
$('#menu li.selected ul').show(); // drop down selected item's sub-menu
$('#menu li a').click(function() {
var speed = 'fast';
var checkElement = $(this).next();
if (checkElement.is('ul')) {
if (!checkElement.is(':visible')) {
$('#menu ul:visible').slideUp(speed, function() {
checkElement.slideDown(speed).parent().addClass('open');
}).parent().removeClass('open');
}
return false;
}
});
});
};
The animations for jQuery can be linked sequentially using the callbacks for the functions. 可以使用函数的回调顺序地链接jQuery的动画。 So for example 所以举个例子
$("#menu").slideUp("fast", function () {
$("#menu2").slideUp("fast", function () {
$("#menu3").slideDown("slow");
});
});
This little snippet will roll up #menu
, then roll up #menu2
, and finally roll down #menu3
这个小片段将卷起#menu
,然后卷起#menu2
,终于滚下来#menu3
Check the documentation for some examples: http://api.jquery.com/slideUp/ 查看文档中的一些示例: http : //api.jquery.com/slideUp/
Also, this question might be of use: jQuery animation queues across multiple elements 另外,这个问题可能有用: jQuery动画跨多个元素排队
I'd do this using dynamic classes on the elements to flag them as being expanded or collapsed. 我会在元素上使用动态类来将其标记为展开或折叠。 So, for example. 因此,例如。
$("#menu ul").click( function() {
$("#expanded").slideUp( function() {
$("#expanded").removeClass("expanded");
$this.slideDown();
$this.addClass("expanded");
});
});
that probably won't work verbatim as I've not tested it, but it shows the principle. 因为我没有测试它,所以它可能无法按原样工作,但是它说明了原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.