简体   繁体   English

手风琴打开/关闭所有内容按钮等等

[英]Accordion open/close all content button and more

I have a responsive accordion function inside a website and i want to (open) and (close) all content with one button that also change his content name to (open) when all content is closed and (closed) when all content is open. 我在网站内具有响应式手风琴功能,我想用一个按钮来(打开)和(关闭)所有内容,并且在关闭所有内容时也将其内容名称更改为(打开),并在所有内容打开时将其内容名称更改为(关闭)。

Also now the content that already was opened closes again when using the (open) button and the plus and minus icons don't react the right way showing the (minus icon) when the content is closed and visa versa. 同样,现在当使用(打开)按钮时,已经打开的内容再次关闭,并且当内容关闭时,加号和减号图标无法正确显示(减号)图标,反之亦然。

Here is the fiddle 这是小提琴

Can someone help me with this? 有人可以帮我弄这个吗?

// Accordion //

$('.header').click(function(){
    $('.content',$(this).parent()).slideToggle();
    $(this).toggleClass('active');
})

$('.toggle-btn').click(function(){
    $('.content').slideToggle();
    $(this).toggleClass('active');
})

If you check for the class active after the toggle occurs, you can then change the text of the button depending on if the toggled class is active or not. 如果在切换发生后检查active类别,则可以根据切换的活动类别是否激活来更改按钮的文本。

See this updated fiddle (edited to change the icons also) 查看此更新的小提琴 (也已编辑以更改图标)

To change your minus/plus icone with your button, you must select specific .header class with parent() and child() jQuery method like this : 要使用按钮更改减号/加号图标,您必须选择具有parent()和child()jQuery方法的特定.header类,如下所示:

$('.toggle-btn').click(function(){
    $('.content').each( function() { 
    $(this).slideToggle();
    $(this).parent().find('.header').toggleClass('active');
     });
});

There you go: http://jsfiddle.net/w3srayj6/21/ 你去了: http : //jsfiddle.net/w3srayj6/21/

// Accordion //

$(document).ready(function() {

    $('.header').click(function(){
        $('.content',$(this).parent()).slideToggle();
        $(this).toggleClass('active');
         $('.toggle-btn').addClass('active').trigger("change"); 
    })

});

$(document).ready(function() {
    $('.toggle-btn').change(function(){
        var headers = $('.header');
        var state = 'open';
        $.each(headers,function(){
            if($(this).hasClass('active'))
                state = 'close';
        });
        if(state == 'open')
            $(this).addClass('active')
        $(this).text(state);
    });
    $('.toggle-btn').click(function(){
        var current = $(this);
        current.toggleClass('active');
        current.trigger("change");
        var contents = $('.content');
        $.each(contents, function(){
            if(!current.hasClass('active'))
                $(this).slideUp();
            else
                $(this).slideDown();
        });
        var headers = $('.header');
        $.each(headers, function(){
            if(current.hasClass('active'))
                $(this).addClass('active');
            else
                $(this).removeClass('active');
        });      
       current.trigger("change");
    })

});

// Read more //

$(window).scroll(function() {
    if ($(this).scrollTop() < 20) {
        $('.read-more').slideDown(200);
    } else {
        console.log('there');
        $('.read-more').slideUp(200);
    }
});

Sometimes working with toggles may be a bit tricky and confusing. 有时使用拨动开关可能会有些棘手和混乱。

In this case I used "hasClass" in order to determine if items are already open or not. 在这种情况下,我使用“ hasClass”来确定项目是否已经打开。 Since we have only "opened" and "closed" states, we can say that as long that "open" is not "Active" (has class active on it), we should add the "active" class flag to all headers and contents. 由于我们只有“打开”和“关闭”状态,因此可以说,只要“打开”不是“活动”(类处于活动状态),就应该在所有标头和内容中添加“活动”类标志。 same in the opposite situation. 相反的情况也一样。 this makes sure that already toggled items are not re-toggled. 这样可以确保已切换的项目不会被重新切换。

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

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