jQuery stop(true, true) 跳转到队列中所有动画的末尾

2022-01-21 00:00:00 queue animation jquery javascript

我一直在使用 jQuery 的 stop(true, true) 方法来清除正在运行的动画,以便下一个立即开始.我注意到第一个参数 clearQueue 清除了整个动画队列,但第二个参数 jumpToEnd 只跳转到当前正在运行的动画的末尾,而不是那些已从队列中删除.有没有办法让它清晰并跳转到所有排队动画的末尾?

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, clearQueue, clears the entire animation queue but the second parameter, jumpToEnd, only jumps to the end of the currently running animation and not the ones that were removed from the queue. Is there a way to have it clear and jump to the end of all queued animations?

我最终链接了几个 stop(false, true) 调用,但是这只会处理 3 个排队的动画,例如.

I've ended up chaining a few stop(false, true) calls instead, but this will only handle 3 queued animations, for example.

$(this)
  .stop(false, true)
  .stop(false, true)
  .stop(false, true)
  .addClass('hover', 200);

根据 Ates Goral 的回答,我最终添加了自己的方法 stopAll:

I ended up adding my own method, stopAll, as per Ates Goral's answer:

$.fn.extend({ stopAll: function () {
    while (this.queue().length > 0)
      this.stop(false, true);
    return this;
  } });

$(this).stopAll().addClass('hover', 200);

我希望其他人觉得这很有用.

I hope someone else finds this useful.

推荐答案

jQuery 1.9 引入了 .finish() 方法,正是实现了这一点.

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

相关文章