jQuery stop(true, true) to jump to end of all animations in queue

前端 未结 4 1492
野性不改
野性不改 2021-02-14 08:19

I have been using jQuery\'s stop(true, true) method to clear running animations so the next one starts immediately. I noticed that the first parameter, clearQ

相关标签:
4条回答
  • 2021-02-14 08:39

    jQuery 1.9 introduced the .finish() method, which achieves exactly that.

    0 讨论(0)
  • 2021-02-14 08:51

    I also notice from the documentation of the .finish() method in jQuery 1.9 that

    Animations may be stopped globally by setting the property $.fx.off 
    to true. When this is done, all animation methods will immediately 
    set elements to their final state when called, rather than displaying an effect.
    

    There is also a nice demo of the various methods on the .finish() documentation page.

    0 讨论(0)
  • 2021-02-14 08:59

    Chaining multiple stop(false, true) makes sense. Instead of hard-coding a fixed number of chained calls, you could check the queue length:

    while ($(this).queue().length) {
        $(this).stop(false, true);
    }
    

    Demo: http://jsfiddle.net/AB33X/

    0 讨论(0)
  • 2021-02-14 09:04

    Test for presence of class (set upon hover and removed on mouseOut animate callback) before staring new animation. When new animation does start, dequeue.

    $("div").hover(function(){
        if (!$(this).hasClass('animated')) {
            $(this).dequeue().stop().animate({ width: "200px" });
        }
    }, function() {
        $(this).addClass('animated').animate({ width: "100px" }, "normal", "linear", function() {
            $(this).removeClass('animated').dequeue();
        });
    });
    
    0 讨论(0)
提交回复
热议问题