在仔细考虑了这一点之后,我意识到由于 jQuery 中处理动画的方式,您要尝试做的事情并不容易。
由于动画是由一个队列管理的,所以如果它们不在同一个函数中,就不可能在同一个元素上运行并发动画。
也就是说,
$(element).animate(aThing, aTime);
.animate(anotherThing, anotherTime);
不会并行运行。 aThing 将在 aTime 内结束,随后 anotherThing 将持续 anotherTime。
因此,您只能通过将它们放在同一个函数中来完成多个更改:
$(element).animate({aThing: aValue, anotherThing: anotherValue}, aTime);
这里简要解释一下在 jQuery 中如何处理动画函数。
在动画期间为元素分配一个计时器对象:
function t( gotoEnd ) {
return self.step(gotoEnd);
}
t.elem = this.elem;
jQuery.timers.push(t);
当你调用停止函数时,它会从定时器中移除定时器:
// go in reverse order so anything added to the queue during the loop is ignored
for ( var i = timers.length - 1; i >= 0; i-- ) {
if ( timers[i].elem === this ) {
if (gotoEnd) {
// force the next step to be the last
timers[i](true);
}
timers.splice(i, 1);
}
}
因此,由于计时器本身已被终止,因此无法删除动画函数的特定属性。
我能想到的唯一方法是跟踪开始时间和持续时间,重新排队动画并停止当前动画。
var start = $.now();
var duration = 5000;
$(element).animate({opacity: '0.5', width: '500px'}, duration);
...
var remaining = duration - ($.now() - start);
$(element).animate({opacity: '0.5', remaining)
.stop();