如何在下一个动画开始之前等待一个jquery动画完成?
我有以下 jQuery:
$("#div1").animate({ width: '160' }, 200).animate({ width: 'toggle' }, 300 );
$("#div2").animate({ width: 'toggle' }, 300).animate({ width: '150' }, 200);
我的问题是两者同时发生。我希望 div2 动画在第一个动画完成时开始。我尝试过下面的方法,但它做了同样的事情:
$("#div1").animate({ width: '160' }, 200).animate({ width: 'toggle' }, 300, ShowDiv() );
....
function ShowDiv(){
$("#div2").animate({ width: 'toggle' }, 300).animate({ width: '150' }, 200);
}
如何让它等待第一个完成?
I have the following jQuery:
$("#div1").animate({ width: '160' }, 200).animate({ width: 'toggle' }, 300 );
$("#div2").animate({ width: 'toggle' }, 300).animate({ width: '150' }, 200);
My issue is that both happen at the same time. I would like the div2 animation to start when the first one finishes. I've tried the method below, but it does the same thing:
$("#div1").animate({ width: '160' }, 200).animate({ width: 'toggle' }, 300, ShowDiv() );
....
function ShowDiv(){
$("#div2").animate({ width: 'toggle' }, 300).animate({ width: '150' }, 200);
}
How can I make it wait for the first one to finish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
http://api.jquery.com/animate/
animate 具有“完整”的功能。您应该将第二个动画放置在第一个动画的完整功能中。
编辑:示例 http://jsfiddle.net/xgJns/
http://api.jquery.com/animate/
animate has a "complete" function. You should place the 2nd animation in the complete function of the first.
EDIT: example http://jsfiddle.net/xgJns/
http://jsfiddle.net/joaquinrivero/TWA24/2/embedded/result/
http://jsfiddle.net/joaquinrivero/TWA24/2/embedded/result/
您可以将函数作为参数传递给
animate(..)
函数动画完成后调用。像这样:下面的例子是对参数的更清晰的解释。
You can pass a function as parameter to the
animate(..)
function which is called after the animation completes. Like so:The example below is a clearer explanation of the parameters.
按照 kingjiv 所说,您应该使用
complete
回调来链接这些动画。您几乎在第二个示例中就包含了它,只是您通过在其后面加上括号来立即执行 ShowDiv 回调。将其设置为ShowDiv
而不是ShowDiv()
并且它应该可以工作。mcgrailm 的响应(在我写这篇文章时发布)实际上是同一件事,只是使用匿名函数进行回调。
Following what kingjiv said, you should use the
complete
callback to chain these animations. You almost have it in your second example, except you're executing your ShowDiv callback immediately by following it with parentheses. Set it toShowDiv
instead ofShowDiv()
and it should work.mcgrailm's response (posted as I was writing this) is effectively the same thing, only using an anonymous function for the callback.
不要使用超时,使用完整的回调。
Don't use a timeout, use the complete callback.
这对我有用。我不确定为什么你的原始代码不起作用。也许需要将其封装在匿名函数中?
This works for me. I'm not sure why your original code doesn't work. Maybe it needs to be incased in an anonymous function?