简体   繁体   English

jQuery切换不会在点击时关闭

[英]jquery toggle won't close on click

I have this code for an accordion style toggle. 我有此代码用于手风琴样式切换。 Works great, only problem is, if you click on an open accordion, it slides up then back down. 效果很好,唯一的问题是,如果您单击打开的手风琴,它会先滑后滑。 It doesn't slide closed. 它不会滑动关闭。

Any thoughts? 有什么想法吗?

Thanks! 谢谢!

//toggles 2
$('body').on('click','.toggle h3 a', function(){

    if($(this).parents('.toggles').hasClass('accordion')) return false;

    $(this).parents('.toggles').find('.toggle > div').slideUp(300);
    $(this).parents('.toggles').find('.toggle h3 a i').attr('class','icon-plus-sign');
    $(this).parents('.toggles').find('.toggle').removeClass('open');

    $(this).parents('.toggle').find('> div').slideDown(300);
    $(this).parents('.toggle').addClass('open');

    //switch icon
    if( $(this).parents('.toggle').hasClass('open') ){
        $(this).find('i').attr('class','icon-minus-sign');
    } else {
        $(this).find('i').attr('class','icon-plus-sign');
    }

    return false;
});



<div class="toggles">

<div class="toggle accent-color"><h3><a href="#"><i class="icon-minus-sign"></i>First Accord</a></h3>
    <div>
    Content
    </div>
</div> 

<div class="toggle accent-color"><h3><a href="#"><i class="icon-minus-sign"></i>Second Accord</a></h3>
    <div>
    Content
    </div>
</div> 

It's pretty simple why it does that. 这么做很简单。 Your function always execute slideUp and slideDown. 您的函数始终执行slideUp和slideDown。 In that order. 以该顺序。 So when your div is collapsed, it will execute slideUp(which doesn't really make anything because it's already collapsed) and then it will slideDown. 因此,当您的div合拢时,它将执行slideUp(由于已经合拢,实际上并没有做任何事情),然后将执行slideDown。 Now, when the div is expanded: The div will slideUp (this time it does go Up because it's not collapsed) and then it will go Down. 现在,当div展开时:div将会slideUp(这一次是因为Up没有折叠,所以它确实Up了),然后Down了。

A better way to do it would be with SlideToggle. 更好的方法是使用SlideToggle。

http://api.jquery.com/slidetoggle/ http://api.jquery.com/slidetoggle/

EDIT: Also, you can check if the div is collapsed or not with... 编辑:此外,您可以检查div是否折叠,或者...

if($(element).is(':visible')){
   //expanded
} else {
   //collapsed
}

Sorry I didn't really understand what was happening with your code so I rewrote the js part from scratch: http://jsfiddle.net/d503n47r/2/ 抱歉,我不太了解您的代码正在发生什么,所以我从头重写了js部分: http : //jsfiddle.net/d503n47r/2/

$('body').on('click','.toggle h3 a', function(e){
    e.preventDefault();
    if($(this).parents('.toggles').hasClass('accordion')) return false;

    if ($(this).parents('.toggle').find('div').eq(0).hasClass('open')) {
            $(this).parents('.toggle').find('div').eq(0).removeClass('open').slideUp();
    } else {
            $('.toggle.open').removeClass('open');
            $(this).parents('.toggle').find('div').eq(0).addClass('open').slideDown();
    }

});

Please take a look at jquery UI as it helps a lot with all this common things http://jqueryui.com/ (No need to reinvent the wheel) 请看一下jquery UI,因为它对所有这些常见的事情都有很大帮助http://jqueryui.com/ (无需重新发明轮子)

MinusFour was correct that it was firing both at the same time. MinusFour是正确的,它同时发射两个。

A way to remedy this is to do an if / then statement that checks whether or not it's open, and then fire's the open / close dependent on that. 解决此问题的一种方法是执行一个if / then语句,检查它是否打开,然后触发依赖于此的打开/关闭。

Here's a link to a JSfiddle 这是JSfiddle的链接

I also added the class "toggle-panel" to the container that was sliding up / down, just so that following would be easier to read. 我还向向上/向下滑动的容器添加了“ toggle-panel”类,以使后续内容更易于阅读。

 if($(this).closest('.toggle').hasClass('open')){
        console.log('open');
        $(this).parents('.toggle').find('.toggle-panel').slideUp(300);
        $(this).closest('.toggle').removeClass('open');
        return
    }
    else {
        console.log('close');
        $(this).parents('.toggles').find('.toggle h3 a i').attr('class','icon-plus-sign');
        $(this).parents('.toggle').find('.toggle-panel').slideDown(300);
        $(this).parents('.toggle').addClass('open');
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM