JQuery 同步动画 [英] JQuery synchronous animation

查看:39
本文介绍了JQuery 同步动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在很多情况下,我希望动画同步执行.尤其是当我想制作一系列连续动画时.

In many cases I wish animation to be executed synchronously. Especially when I wish to make a a series of sequential animations.

是否有一种简单的方法可以使 jQuery animate 函数调用同步?

Is there an easy way to make a jQuery animate function call synchronous?

我想到的唯一方法是在动画完成时设置一个标志为真并等待这个标志.

The only way I thought about is to set a flag true when the animation has finished and to wait for this flag.

推荐答案

jQuery 无法制作同步动画.

jQuery cannot make synchronous animations.

请记住 JavaScript 在浏览器的 UI 线程上运行.

Remember that JavaScript runs on the browser's UI thread.

如果您制作同步动画,浏览器将冻结,直到动画完成.

If you make a synchronous animation, the browser will freeze until the animation finishes.

为什么需要这样做?

您可能应该使用 jQuery 的回调参数并在回调中继续您的方法代码,如下所示:

You should probably use jQuery's callback parameter and continue your method code in the callback, like this:

function doSomething() {
    var thingy = whatever;
    //Do things
    $('something').animate({ width: 70 }, function() {
        //jQuery will call this method after the animation finishes.
        //You can continue your code here.
        //You can even access variables from the outer function
        thingy = thingy.fiddle;
    });
}

这称为闭包.

这篇关于JQuery 同步动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆