【发布时间】:2010-11-08 21:04:09
【问题描述】:
我有多个函数可以对 HTML 的不同部分执行不同的动画。我想链接或排队这些函数,以便它们按顺序而不是同时运行动画。
我正在尝试按顺序自动执行多个事件,以使用户看起来像是在单击不同的按钮或链接。
我可能可以使用回调函数来做到这一点,但是我必须从不同的函数中提取所有动画并以正确的模式重新组合。
jquery“队列”有帮助吗?我无法理解队列中的documentation。
例如,JQuery:
function One() {
$('div#animateTest1').animate({ left: '+=200' }, 2000);
}
function Two() {
$('div#animateTest2').animate({ width: '+=200' }, 2000);
}
// Call these functions sequentially so that the animations
// in One() run b/f the animations in Two()
One();
Two();
HTML:
<div id="animatetest" style="width:50px;height:50px;background-color:Aqua;position:absolute;"></div>
<div id="animatetest2" style="width:50px;height:50px;background-color:red;position:absolute;top:100px;"></div>
谢谢。
编辑: 我用timers 尝试过,但我认为有更好的方法。
编辑#2:
让我更具体一点。我有多个功能绑定到页面不同元素上的单击和悬停事件。通常这些函数彼此无关(它们不相互引用)。我想在不更改现有函数代码的情况下模拟用户经历这些事件。
【问题讨论】:
-
我认为使用 animate 函数提供的回调功能是可行的方法。
-
现在有一种方法可以在 jquery 中使用 promise 系统执行此操作。请在此处查看我的答案:stackoverflow.com/a/18216474/726239
标签: jquery animation queue chaining