jQuery动画延迟问题与自行循环的步骤

Spr*_*tar 9 javascript jquery jquery-animate

我有一个timeline定义,列出选择器以及应用于该对象的延迟和动画列表.您可以指定循环特定对象的步骤.

这是用于对动画进行排队的函数:

function animateWithQueue(e, obj) {
    if ($.queue(e[0]).length == 0) {
        e.queue(function doNext(next) {
            $.each(obj.steps, function(i, step) {
                e.delay(step.pause).animate(step.anim, step.options);
            });
            if (obj.loop) {
                e.queue(doNext);
            }
            next();
        });
    }
}?
Run Code Online (Sandbox Code Playgroud)

这是timeline信息

var timeline = {
    '.square': {
        loop: true,
        steps: [
            { pause: 800, anim: { right: '+=200' }, options: { duration: 400} },
            { pause: 1000, anim: { right: '-=200' }, options: { duration: 400} }
        ]
    },
    '.circle': {
        loop: true,
        steps: [
            { pause: 1200, anim: { top: '+=200' }, options: { duration: 400} },
            { pause: 1200, anim: { top: '-=200' }, options: { duration: 400} }
        ]
    }
};
Run Code Online (Sandbox Code Playgroud)

以下是timeline放入上述animate函数的函数:

$.each(timeline, function(selector, obj) {
    animateWithQueue($(selector), obj);
});
Run Code Online (Sandbox Code Playgroud)

这是一个完整的例子.http://jsfiddle.net/sprintstar/Tdads/

此代码似乎工作正常,可以单击动画循环和停止按钮来停止动画,清除队列等.但是我们面临的问题可以通过点击停止并重复开始(比如说10次)来触发.然后注意延迟不再正常工作,并且形状移动得更快.

这是为什么,以及如何解决?

RYF*_*YFN 1

有些事情不太正常delay...

作为解决方法,我已将其替换为doTimeoutin this fiddle,因此如下:

  e.delay(step.pause).animate(step.anim, step.options);
Run Code Online (Sandbox Code Playgroud)

变成:

    var timerName = e[0].className + $.now();
    timeouts.push(timerName);
    e.queue(function(next) {
      e.doTimeout(timerName, step.pause, function() {
          this.animate(step.anim, step.options);
          next();
        });
    });
Run Code Online (Sandbox Code Playgroud)

timeouts是一组唯一的超时 id - 当按下停止按钮时,每个超时 id 都会被清除。

正如我所说,这更多的是一种解决方法而不是修复,因为您还需要在停止时重置元素的位置。(请注意,我已从顶部/右侧定义中删除了 += 和 -=)