SlideUp 所有 div &使用 jQuery 一键更改 h3 类

发布于 2024-10-11 02:20:15 字数 2162 浏览 5 评论 0原文

我有一个简单的手风琴式机制,我一直在研究。我熟悉slideUp和slideDown的回调功能,并使用它来实现动画完成后类的更改。

我有 2 个按钮 - 一个展开手风琴中的所有 div,另一个折叠它们。这工作得很好,当仅使用这些按钮来控制手风琴时,类会在正确的时间更改。

但是,当我打开其中一个手风琴(通过单击 h3),然后单击按钮折叠所有 div 时 - h3 上的类立即发生变化(因此其外观也发生变化)。

一定是某个地方发生了冲突。我确信我的函数也不是我们专门编写的,但任何帮助将不胜感激。

谢谢

这是 jQuery:

$('.accordion h3').addClass('closed').next('div').hide();

        $('.accordion h3').click( function() {
            if ($(this).hasClass('closed')){
                $(this).removeClass('closed');
                $(this).addClass('open');
        $(this).next('div').stop(true,true).slideDown();
            }   else if ($(this).hasClass('open')){
                    $(this).next('div').stop(true,true).slideUp('normal', function() {
                        $(this).prev('h3').removeClass('open');
                        $(this).prev('h3').addClass('closed');
                    });
                }
        });

        $('#expand-all').click( function() {
            if ($('.accordion h3').hasClass('closed')){
                $('.accordion h3').removeClass('closed');
                $('.accordion h3').addClass('open');
        $('.accordion h3').next('div').stop(true,true).slideDown();
            }
            return false;
        });

        $('#collapse-all').click( function() {
            if ($('.accordion h3').hasClass('open')){
        $('.accordion h3').next('div').stop(true,true).slideUp('normal', function(){
                    $('.accordion h3').removeClass('open');
                    $('.accordion h3').addClass('closed');
                });
            }
            return false;
        });

以下 html 已简化,但基本结构是相同的:

<a id="expand-all" href="#">Expand all</a>
<a id="collapse-all" href="#">Collapse all</a>

<div class="accordion">
    <h3>Clickable to trigger dropdown</h3>
    <div>This is the hidden content that shows when the h3 is clicked</div>
    <h3>Clickable to trigger dropdown</h3>
    <div>This is the hidden content that shows when the h3 is clicked</div>
</div>

I have a simple accordion type mechanism that I've been working on. I am familiar with slideUp and slideDown's callback functionality and have used this to achieve the changing of classes after animations have completed.

I have 2 buttons - one expands all divs in the accordion and the other collapses them. This works fine and when using only those buttons to control the accordion the classes are changed at the correct times.

However, when I open one of the accordions (by clicking on an h3) and then click on the button to collapse all the divs - the class on the h3 immediately changes (and therefore its appearance).

There must be a conflict somewhere. I'm sure my functions are not particularly we written either but any help would be appreciated.

Thanks

This is the jQuery:

$('.accordion h3').addClass('closed').next('div').hide();

        $('.accordion h3').click( function() {
            if ($(this).hasClass('closed')){
                $(this).removeClass('closed');
                $(this).addClass('open');
        $(this).next('div').stop(true,true).slideDown();
            }   else if ($(this).hasClass('open')){
                    $(this).next('div').stop(true,true).slideUp('normal', function() {
                        $(this).prev('h3').removeClass('open');
                        $(this).prev('h3').addClass('closed');
                    });
                }
        });

        $('#expand-all').click( function() {
            if ($('.accordion h3').hasClass('closed')){
                $('.accordion h3').removeClass('closed');
                $('.accordion h3').addClass('open');
        $('.accordion h3').next('div').stop(true,true).slideDown();
            }
            return false;
        });

        $('#collapse-all').click( function() {
            if ($('.accordion h3').hasClass('open')){
        $('.accordion h3').next('div').stop(true,true).slideUp('normal', function(){
                    $('.accordion h3').removeClass('open');
                    $('.accordion h3').addClass('closed');
                });
            }
            return false;
        });

The following html is simplified but the basic structure is the same:

<a id="expand-all" href="#">Expand all</a>
<a id="collapse-all" href="#">Collapse all</a>

<div class="accordion">
    <h3>Clickable to trigger dropdown</h3>
    <div>This is the hidden content that shows when the h3 is clicked</div>
    <h3>Clickable to trigger dropdown</h3>
    <div>This is the hidden content that shows when the h3 is clicked</div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-10-18 02:20:15

这是因为其中一个手风琴已经关闭,因此 SlideUp 已完成,并且正在调用:

$('.accordion h3').removeClass('open');
$('.accordion h3').addClass('closed');

而不是执行 $('.accordion h3'),您应该更改一个相对的,因此请尝试:

$(this).prev('h3').removeClass('open');
$(this).prev('h3').addClass('closed');

在折叠所有 SlideUp 回调中。

It's because one of the accordians is already closed, therefore the slideUp is done and this is calling:

$('.accordion h3').removeClass('open');
$('.accordion h3').addClass('closed');

Rather than doing $('.accordion h3'), you should be changing the one relative, so try:

$(this).prev('h3').removeClass('open');
$(this).prev('h3').addClass('closed');

In the collapse all slideUp callback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文