JQuery的动画同步 [英] JQuery synchronous animation

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

问题描述

在很多情况下,我希望动画将同步执行。尤其是当我想作出一系列连续的动画。

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

有一个简单的方法,使一个jQuery 动画函数调用同步?

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的回调参数,并继续你的方法code回调,像这样的:

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;
    });
}

这就是所谓的闭包。

This is called a closure.

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

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