简体   繁体   English

设置jQuery动画的队列长度

[英]Set a queue length for jQuery animations

Is there a way to set a queue length of animation functions? 有没有办法设置动画功能的队列长度?

Imagine someone fires a jQuery animate function using the arrow keys. 假设有人使用箭头键触发了jQuery动画功能。 Now he dashes the arrow keys like crazy and has to wait for the queued functions to complete which may take a lot of time and may be confusing and nerve-wracking. 现在,他像疯了似的那样划破箭头键,不得不等待排队的功能完成,这可能要花费很多时间,并且可能会令人困惑和不安。 I know you can stop() an animation but I want it to simply have a queue length of let's say three. 我知道您可以stop()动画,但我希望它的队列长度简单地说为3。

Here's a fiddle . 这是一个小提琴 Open it and hit your keys. 打开它,然后按一下键。

Example code: 示例代码:

var div = $(".bouncer");

function runIt() {
div
    .animate({ left: "+=200" }, 500 )
    .animate({ left: "-=200" }, 500 )
}

$(document).keydown(function(e) {
    if (e.keyCode) {
        runIt();
    }
});

As of i know there is no direct method to do that, but try this : 据我所知,没有直接的方法可以做到这一点,但是试试这个:

var div = $(".bouncer");

function runIt() {
div.animate({left: "+=200" }, 500)
   .animate({left: "-=200" }, 500)
}

$(document).keydown(function (e) {
if (e.keyCode) {
    var len=div.queue("fx").length;
    if(len<=1)
      runIt();
    }
});

jsfiddle.net/kcCG5/4/ jsfiddle.net/kcCG5/4/

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

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