简体   繁体   English

等待动画完成,然后继续使用jQuery

[英]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.

相关问题 [javascript,jQuery]如何在继续播放另一个没有回调的动画之前等待动画完全完成 - [javascript, jQuery]How to wait for the animation to complete entirely before continuing another animation without callbacks 在继续之前等待循环完成 - Wait for for loop to complete before continuing 离子等待方法完成,然后再继续 - Ionic wait for method to complete before continuing 等待异步任务完成,然后继续吗? - Wait for asynchronous task to complete before continuing? jQuery:等待CSS动画完成,然后再更改另一个CSS属性 - jQuery: wait for css animation to complete before changing another css attribute Jquery 等待 Function 的返回值后再继续 - Jquery Wait for Return Value of Function before Continuing 如何在继续之前等待 cordova.plugins.sqlitePorter.exportDbToSql 完成? - How to wait cordova.plugins.sqlitePorter.exportDbToSql to complete before continuing? NodeJS - 等到流式传输多个文件完成后再继续编写代码 - NodeJS - Wait Until Streaming Multiple Files Is Complete Before Continuing Code 强制$ .each()循环等待动画完成后再继续 - Force $.each() loop to wait for animations to complete before continuing 函数是否在继续执行之前等待被调用函数完成? - Does a function wait for a called function to complete before continuing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM