简体   繁体   English

向上滑动和向下滑动问题

[英]Slide Up and Slide Down issue

Why does this work, this just says when clicked slide the one that was clicked down and all other siblings slide up. 为什么要这样做,这只是说单击时将被单击的对象向下滑动而所有其他同级的对象向上滑动。

(function(){
    var dd = $('dd');
    var dt = $('dt');

    dd.filter(':nth-child(n+4)').hide();

    dt.on('click', dt , function(){
        $(this).next()
            .slideDown(200).  //slide down first works
            siblings('dd').slideUp(200);
    });

})();

but not this. 但不是这个。 This one just says the one that was clicked, fist hide the siblings and then show the one that was clicked, but it dosent work well and im having trouble understanding why. 这个只是说一个被点击的拳头,将兄弟姐妹藏起来,然后显示一个被点击的拳头,但是它工作得很好并且我很难理解为什么。

(function(){
    var dd = $('dd');
    var dt = $('dt');

    dd.filter(':nth-child(n+4)').hide();

    dt.on('click', dt , function(){
        $(this).next()
            .siblings('dd').slideUp(200)  //slide down is now second
            .slideDown(200);
    });

})();

Here is a link of the none working one 这是一个无效的链接

http://jsfiddle.net/#&togetherjs=CoLBMUmtSk http://jsfiddle.net/#&togetherjs=CoLBMUmtSk

You're chaining it on the siblings to "slideDown" even though you've just told them to "slideUp" 即使您只是告诉他们“ slideUp”,您也将其链接到“ slideDown”

Javascript: 使用Javascript:

(function () {
    var dd = $('dd');
    var dt = $('dt');

    dd.filter(':nth-child(n+4)').hide();

    dt.on('click', dt, function () {
        $(this).next()
            .siblings('dd').slideUp(200);
        $(this).next().slideDown(200);
    });

})();

That should work. 那应该工作。

In your first piece of code all the animations happen at the same time, because you are calling the slideUp and slideDown functions without delays or callbacks. 在您的第一段代码中,所有动画都同时发生,因为您正在调用slideUpslideDown函数而没有延迟或回调。 The second code does not work because you are first calling the siblings function, and then calling slideUp and slideDown on the result of this, so never really acting on the item itself. 第二个代码不起作用,因为您首先要调用siblings函数,然后根据此结果调用slideUpslideDown ,因此切勿真正作用于项目本身。

If you want to first hide the siblings, then show the item you want you can use the following: 如果要先隐藏同级,然后显示所需项,可以使用以下方法:

(function () {
    var dd = $('dd');
    var dt = $('dt');

    dd.filter(':nth-child(n+4)').hide();

    dt.on('click', dt, function () {
        var next = $(this).next();
        next.siblings('dd').slideUp(1000);
        next.delay(1000).slideDown(1000);
    });

})();

I have increased the time to make it clear that one action (slideUp) is happening before the other (slideDown). 我增加了时间来清楚地说明一个动作(slideUp)在另一个动作(slideDown)之前发生。

Like i explained in your code example live, this would work as you intended: 就像我在您的代码示例实况中解释的那样,它将按您的预期工作:

(function(){
    var dd = $('dd');
    var dt = $('dt');

    dd.filter(':nth-child(n+4)').hide();

    dt.on('click', dt , function(){
        dd.slideUp(200);

        $(this).next('dd').slideDown(200);
    });

})();

Using siblings for this seems like an overkill, and jQuery is notoriously bad at handling multiple animations of the same elements at once. 为此使用兄弟姐妹似乎是一种矫kill过正,并且众所周知,jQuery难以一次处理相同元素的多个动画。

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

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