jquery 为多个元素设置动画
我正在尝试执行以下操作:
我在页面上有两组 DOM 元素。第一组中的所有元素都带有 display-element
类,第二组元素带有 edit-element
类。
现在我有一个按钮(id=edit-button
)。当我单击此按钮时,我希望所有 display-elements
淡出,并显示所有 edit-elements
。
我自然而然地这么做了:
$('#edit-button').click(function(){
$('.display-element').fadeOut(300, function() {
$('.edit-element').fadeIn(300);
});
});
我惊讶地发现这并没有按预期工作。事情是这样的:一旦第一个带有 display-element
类的元素淡出,所有 edit-elements
就开始淡入。
现在有没有简单的方法(也许我错过了文档中的某些内容),使用它我可以让所有 edit-elements
仅在所有 display-elements
淡出后才开始淡入? >
谢谢
杰瑞赫
I'm trying to do the following:
I have two sets of DOM elements on a page. All elements in the first set carry the class display-element
, and the second set of elements carry the class edit-element
.
Now I have a button (id=edit-button
). When I click this button, I want all the display-elements
to fade out, and all the edit-elements
to appear.
I naturally did:
$('#edit-button').click(function(){
$('.display-element').fadeOut(300, function() {
$('.edit-element').fadeIn(300);
});
});
I found to my surprise that this did not work as expected. This is what happened: as soon as the first element with class display-element
faded out, all the edit-elements
started fading in.
Now is there any easy way (perhaps something in the documentation I missed) using which I can have all the edit-elements
start fading in only after all display-elements
have faded out?
Thanks
jrh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,由于没有找到任何“漂亮的 API 方法”,我决定这样做:
这本质上是 @shylent 的答案,还有更多“javascript-fu”:)
Well, having not found any 'nifty API method', I resolved to this:
This is essentially @shylent's answer, with more 'javascript-fu' :)
为什么会发生这种事,真的吗?我的意思是,看起来您正在同时启动动画(毕竟只有一次对
.fadeOut
的调用,对吧?)。我想,这是因为 $() 生成一个数组,当您调用.fadeOut
时,jQuery 会迭代该数组并执行它对其每个元素执行的任何操作。从第一个开始。由于回调会“附加”到每个动画,因此您会看到第一个回调的可见效果。我想,您可以通过自己迭代返回元素的数组并确保仅将回调附加到最后一个元素来回避这一问题。
如果代码关闭,我很抱歉,我必须承认,我的 javascript-fu 不是很强:)
Why would this happen, really? I mean, it appears, that you are starting the animations at the same time (there is only one call to
.fadeOut
after all, right?). I guess, this is because $() yields an array and when you call.fadeOut
, jQuery iterates through the array and performs whatever it performs for each of its elements. Starting from the first. Since the callback gets "attached" to every animation, you see the visible effect of the first callback.I guess, you could sidestep this by iterating through the array of the returned elements yourself and making sure, that you attach the callback only to the last one.
I am sorry if the code is off, I must admit, that my javascript-fu is not very strong :)
解决方案是在淡入函数上使用 setTimeout
该 javascript 函数会将淡入函数的触发延迟 300 毫秒
The solution is to use a setTimeout on the fading in function
That javascript function will delay the trigger of the the fading in function for 300 miliseconds
我刚刚运行了一个类似的问题,发现“step”回调选项产生了所需的行为。
^ http://api.jquery.com/animate/
向下滚动到演示以了解步骤用法并您将看到它如何使您能够在单个对象上调用动画,然后使用步骤在动画的每个步骤中更新您的其余集。这样,当动画完成时,您只会触发一个回调。
I just ran a similar issue and found the "step" callback option yielded the desired behavior.
^ http://api.jquery.com/animate/
Scroll down to the demo for step usage and you'll see how it will enable you to call animate on a single object then use step to update the rest your set at each step of the animation. This way you only have one callback fired when the animation completes.