Making JQuery horizontal accordion close on click

蓝咒 提交于 2019-12-25 14:12:41

问题


Example: http://vincent-massaro.com/map/

Currently, the script allows you to click on a piece of the accordion to open it, but it is set to close on mouseleave. When I set the mouseleave to .click, it gets confused and doesn't know what state it is in. I want to make it so that you can click to open it, and click to close it, instead of mouseleave. The code controlling this is below, and the full script is in haccordion.js linked in the page source. If someone could help me modify this script, I would be very grateful! Thanks in advance.

$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)
  })
  }

回答1:


try use this

$('.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');
});



回答2:


You could set a boolean variable to represent whether the accordion is open or not and just check it on click. (You'll need to toggle the variable's state on click too)

Edit:

Ok try this. Set a boolean global variable (outside the click event) like this:

var accordion_expanded = false;

Then within your click event do something like this: (I haven't tested this so you might have to massage it a bit to fit your circumstance)

In the function where you expand your accordion put this:

accordion_expanded = true;

And in the function where you contract your accordion do a

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

   accordion_expanded == false;
}

Good Luck!



来源:https://stackoverflow.com/questions/2588493/making-jquery-horizontal-accordion-close-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!