可以通过编程方式链接jquery动画吗? [英] Can jquery animations be chained programmatically?

查看:85
本文介绍了可以通过编程方式链接jquery动画吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

jQuery('#flash').animate({opacity: 0.35}, 200)
                .animate({opacity: 0}, 200)
                .animate({opacity: 0.35}, 200)
                .animate({opacity: 0}, 200)
                .animate({opacity: 0.35}, 200)
                .animate({opacity: 0}, 600)

并且我不确定要更改其状态的次数.有没有一种方法可以通过编程方式链接动画,而不必通过编辑动画链来添加/删除链元素?

and I'm not decided on how many times I want its state altered. Is there a way to chain animations programmatically instead having to add/remove chain elements by editing the animate chain?

推荐答案

不,您不能在不编辑动画队列的情况下链接动画.如果要链接变量,但次数有限,则可以轻松地执行循环:

No, you can't chain animations without editing the animation queue. If you want to chain a variable, but limited number of times you can do easily with a loop:

var flash = $("#flash");
for (var i=0; i<n; i++)
    flash.animate({opacity: 0.35}, 200).animate({opacity: 0}, 200);

如果您想要一个无休止的循环,或者在将来要满足某个条件时停止循环,则希望在动画队列上挂一个回调,重新启动该函数:

If you want an endless loop, or one that stops when a condition is to be fulfilled in the future, you want to hook a callback on the animation queue, restarting the function:

var flash = $("#flash");
function anim() {
    // if (condition)
    flash.animate({opacity: 0.35}, 200).animate({opacity: 0}, 200, anim);
                                            // call "recursively": ^^^^
}
anim();

这篇关于可以通过编程方式链接jquery动画吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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