[英]Wait for animation to complete before continuing in jQuery
Is there anyway to wait for a jQuery animation to finish before proceeding with another command? 无论如何,在继续执行另一个命令之前,是否要等待jQuery动画完成?
For example, I want to fade out/slide up an element, change some items within the element and then fade them back in/slide back down. 例如,我要淡出/向上滑动元素,更改元素中的某些项目,然后将它们淡入/向下滑动。
If I put the statements one after another, you can see the items change before the animation completes. 如果我将这些语句一个接一个地放置,则可以在动画完成之前看到项目更改。
$('#item').fadeOut();
$('#item').html('Changed item');
$('#item').fadeIn();
Thanks. 谢谢。
You can pass callback in fadeIn/fadeOut
. 您可以在
fadeIn/fadeOut
传递回调。 jQuery docs jQuery文档
$('#item').fadeOut('slow', function(){
$('#item').html('Changed item');
$('#item').fadeIn();
});
You can either use the callback method which gets called from .fadeOut
, like 您可以使用从
.fadeOut
调用的回调方法,例如
$('#item').fadeOut('fast', function() {
$(this).html('Changed item').fadeIn();
});
or use the underlaying Deferred object 或使用底层的Deferred对象
$('#item').fadeOut('slow').promise().done(function() {
$(this).html('Changed item').fadeIn();
});
Pass a callback function to fadeOut
. 将回调函数传递给
fadeOut
。
$("#item").fadeOut(function() {
$(this).html("changed").fadeIn();
});
This is the sort of thing you'll learn in a basic jQuery tutorial. 这是您将在基本的jQuery教程中学到的东西。
$('#item').fadeOut('slow', function() { // do your stuff here });
有关更多信息: http : //docs.jquery.com/Effects/fadeOut#speedcallback
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.