jQuery 队列,它到底是如何工作的?
我很难弄清楚队列在 jQuery 中的工作原理,尤其是 fx
队列。我想做的是:
依次对多个不同元素进行动画
看看这个首先小提琴。当您单击该按钮时,两个元素都会被动画化。现在我知道我可以做类似 this fiddle 的事情,我将动画放在第一个动画的回调中,但这不是一个选择,因为我有一个情况,我有一个动画池,并且我想以特定的顺序任意运行它们中的任何一个。例如,我可能有动画 A、B、C、D 和 E,它们都为不同的元素设置动画。有时我想按照 B->A->C 的顺序执行,有时我可能想按 A->B->C 的顺序执行。
如何使用队列来实现此功能?
I'm having a hard time figuring out how queue's work in jQuery, especially the fx
queue. What I want to do is:
Animate multiple different elements one after another
Take a look at this fiddle first. When you click on the button, both of the elements are being animated. Now I know I can do something like this fiddle, where I put the animation in the callback of the first animation, but this isn't an option because I have a situation where I have a pool of animations, and I want to arbitrarily run any of them in a particular sequence. For example, I might have animations A, B, C, D, and E which all animate different elements. Sometimes I will want to execute in the order B->A->C, another time I might want A->B->C.
How can I use queues to achieve this functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您的动画正在移动不同的元素,因此管理自己的队列可能比使用 jquery 的队列更好。您可以将要运行的动画存储在数组中。
然后在每个动画结束时调用运行下一个动画的函数。喜欢:
请参阅这个小提琴。
Since your animations are moving different elements, you might be better off managing your own queue than doing it with jquery's. You could store the animations you want to run in an array.
and then at the end of each animation call the function which runs the next one. Like:
See this fiddle.
假设所有 A、B、C...N 都具有相同的类,只是为了在数组中选择它们。
每个元素都有您想要使用的动画属性。
就像我们有一个
DIV
:然后选择所有 DIV 就会像这样。
现在有一个 jQuery shuffle 插件,可以帮助您打乱数组已选择。
现在您可以使用 .each() 函数单独制作动画。
Lets say that you have all the A,B,C...N have the same class just to select them in an array.
And each element has its animation property that you want to do with that.
Like we have a
DIV
s :Then selecting all the DIVs will be like.
Now there is a jQuery shuffle plugin that will help you in shuffling the array you have selected.
Now You can use
.each()
function to animate individually.更新(工作)
下面的代码可以满足您的需求;它基于 Leopd 的自定义队列。但它不使用 jQuery 的队列或延迟对象: http://jsfiddle.net/rkw79/FPGrj/
划掉的代码是使用延迟对象的原始解决方案。它不会产生 OP 所需的功能。它只是为了缩短将多个动画链接到单个对象所需的代码量;它不用于跨多个对象链接动画
您想要使用延迟对象。对于您的情况,这特别容易,因为动画已经创建 promise 对象: http://jsfiddle.net/rkw79/fpseA/Update (working)
The code below does what you want; it is based off of the custom queue from Leopd. It does not use jQuery's queues or deferred objects though: http://jsfiddle.net/rkw79/FPGrj/
The crossed out code was the original solution using deferred objects. It does not produce the functionality desired by the OP. It just serves to shorten the amount of code needed to chain multiple animations to a single object; it does not serve to chain animations across multiple objects
You want to make use of deferred objects. For your case, it is especially easy since animations are already creating promise objects: http://jsfiddle.net/rkw79/fpseA/