[英]Jquery animate callback start next animate
How can I do this more simple?? 我该如何做得更简单?
var i = 0;
$(this).find(".ui-stars-star-on-large:eq("+i+")").animate({width: w+'px'}, 200, "swing", function() {
i++;
$(this).parent().find(".ui-stars-star-on-large:eq("+i+")").animate({width: w+'px'}, 200, "swing", function() {
i++;
$(this).parent().find(".ui-stars-star-on-large:eq("+i+")").animate({width: w+'px'}, 200, "swing", function() {
i++;
$(this).parent().find(".ui-stars-star-on-large:eq("+i+")").animate({width: w+'px'}, 200, "swing", function() {
i++;
$(this).parent().find(".ui-stars-star-on-large:eq("+i+")").animate({width: w+'px'}, 200, "swing", function() {
i++;
});
});
});
});
});
Assuming the things you want to animate are all under the parent, you could name your animate callback...then you can trigger the animation again from inside itself. 假设要设置动画的对象都在父对象下,则可以命名动画回调...然后可以从自身内部再次触发动画。
var $stars = $(this).children('.ui-stars-star-on-large');
var i = 0;
// wrap this whole block in a named function expression, so it can be re-called
(function nextStar() {
// if you want the animations to loop, remove the if
// and add a `i %= $stars.length;` after the jquery stuff
if (i < $stars.length) {
// .eq(i) works much like the ':eq('+i+')' selector, except
// it's less hideous and doesn't require building selectors. :P
$stars.eq(i++).animate({width: w+'px'}, 200, 'swing', nextStar);
}
// end the function and immediately call it, starting the first animation
})();
You could use recursion: 您可以使用递归:
var w = 200;
function animate(count, i) {
$(this).find(".ui-stars-star-on-large:eq(" + i + ")").animate({
width: w + "px"
}, 200, "swing", function () {
i++;
if (i < count) animate(count, i);
}}
}
animate(5, 1);
OR use delay: 或使用延迟:
for(var i = 0; i < 5; i++) {
$(this).find(".ui-stars-star-on-large:eq(" + i + ")")
.delay(i * 200)
.animate({
width: w + "px"
}, 200, "swing")
}
You can just use cycle with setTimeout
if your animation is light and have a good performance 如果您的动画较轻且效果良好,则可以将setTimeout
与cycle一起使用
var $this = $(this),
$stars = $this.parent().find(".ui-stars-star-on-large");
for (var i = 0; i < 5; i++) {
var _i = i;
setTimeout(function(){
$stars().eq(_i).animate({width: w+'px'}, 200, "swing")
}, 200*i);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.