jQuery - 在运行fadeIn之前等待fadeOut完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8207897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:12:23  来源:igfitidea点击:

jQuery - Waiting for the fadeOut to complete before running fadeIn

jquery

提问by BigJobbies

I was wondering if the is any way to wait until the fadeOut is complete before the fadeIn commences, as when i run the following code, it places one div under the other, then when its faded out it moves up ... looks a little ugly.

我想知道在淡入开始之前是否有任何方法可以等到淡出完成,因为当我运行以下代码时,它将一个 div 放在另一个下方,然后当它淡出时它向上移动......看起来有点丑陋的。

Code is as follows:

代码如下:

$('.sidebarform').fadeOut('slow');
$('.sidebarsuccess').fadeIn('slow');

回答by mu is too short

The fadeOutfunction has a callback that it executes when the animation is done:

fadeOut函数有一个回调,它在动画完成时执行:

$('.sidebarform').fadeOut('slow', function() {
    $('.sidebarsuccess').fadeIn('slow');
});

回答by kravits88

Another option is using promise which will wait for all pending animations on .sidebarform to complete first even if they were started elsewhere:

另一种选择是使用 promise,它会等待 .sidebarform 上的所有挂起动画首先完成,即使它们是在其他地方启动的:

$('.sidebarform').fadeOut('slow').promise().done(function() {
    $('.sidebarsuccess').fadeIn('slow');
});

回答by BananaAcid

I am using jQuery's Queues - allows you to put anything on the fxstack (and canceling works with it as well, no need for a finalizing .promise()):

我正在使用 jQuery 的 Queues - 允许您将任何内容放在fx堆栈上(并且取消也可以使用它,无需完成.promise()):

$('.sidebarform').fadeOut('slow').queue(function(done) {
    $('.sidebarsuccess').fadeIn('slow');
    done();
}) .... other chained effects

if no further effects are used, done()can be removed. .queue()will hold the stack until done()is called - useful for async execution. It will not wait for fadeIn.

如果没有使用进一步的效果,done()可以删除。.queue()将保持堆栈直到done()被调用 - 对异步执行很有用。它不会等待淡入淡出。

This will force the fxstack to wait for fadeIn as well (just as an additional example):

这将强制fx堆栈也等待淡入淡出(仅作为附加示例):

$('.sidebarform').fadeOut('slow').queue(function(done) {
    $('.sidebarsuccess').fadeIn('slow', done);
}) .... other chained effects kicking in after fadeIn

The queue will only continue, when the fadeIn completes and calls done as its callback - which is the queues one. The stack sees this as one entry.

只有当淡入淡出完成并调用 done 作为其回调时,队列才会继续——这是队列之一。堆栈将此视为一个条目。