单击即可使JQuery水平手风琴关闭

示例: http : //vincent-massaro.com/map/

目前,该脚本允许您单击手风琴的一部分以打开它,但它设置为在mouseleave上关闭。 当我将mouseleave设置为.click时,它会感到困惑,并且不知道它处于什么状态。我想让它可以点击打开它,然后单击关闭它,而不是mouseleave。 控制它的代码如下,完整脚本位于页面源中链接的haccordion.js中。 如果有人可以帮我修改这个脚本,我将非常感激! 提前致谢。

$target.click(function(){ haccordion.expandli(config.accordionid, this) config.$lastexpanded=$(this) }) if (config.collapsecurrent){ //if previous content should be contracted when expanding current $target.mouseleave(function(){ $(this).stop().animate({width:config.paneldimensions.peekw}, config.speed) }) } 

试试这个

 $('.accordion-item-header').click(function() { var item = $(this).parent().find('.accordion-item-body'); item.toggleClass('open').animate({ width:item.hasClass('open') ? 0: 100 }, 100).toggleClass('open'); }); 

您可以设置一个布尔变量来表示手风琴是否打开,只需点击即可查看。 (您还需要在点击时切换变量的状态)

编辑:

好的,试试吧。 设置一个布尔全局变量(在click事件之外),如下所示:

 var accordion_expanded = false; 

然后在你的点击事件中做这样的事情:(我没有测试过,所以你可能需要按摩它以适应你的环境)

在你扩展你的手风琴的function中把这个:

 accordion_expanded = true; 

在你收缩手风琴的function中

 if(accordion_expanded == true){ //Contract accordion code goes here accordion_expanded == false; } 

祝好运!