jquery - 如何确定最后一个动画元素
我正在使用 jQuery 的 animate 函数对一系列匹配元素进行动画处理,但我想在调用新函数之前确定最后一个匹配元素何时完成动画处理。我该怎么做?有没有办法在没有 .each() 函数的情况下做到这一点?
这是我的代码:
使用每个函数:
var imageLen = $('.option_image').length -1; //Determine index of total images
$('.option_image').each(function(index){
var imageDelay = index*delayTime;
$(this).delay(imageDelay).animate({ 'backgroundPositionY' : '0px', 'opacity' : '1'},fadeTime,function(){
$('.option_title').css('opacity','1');
if (index == imageLen){
//If all have been iterated through - perform this function
}
})
});
但我想做的是:
$('.option_image').animate({ 'backgroundPositionY' : '0px', 'opacity' : '1'},fadeTime,function(){
if (index == imageLen){
$('.option_title').css('opacity','1');
$('#back').fadeTo(fadeTime,1);
$('#restart').fadeTo(fadeTime,1);
}
});
这显然不起作用,因为“索引”未定义。
I'm using jQuery's animate function to animate a series of matching elements, but I want to determine when the last matching element has finished animating before calling a new function. How would I do this? Is there a way to do it without the .each() function?
This is my code:
Using Each Function:
var imageLen = $('.option_image').length -1; //Determine index of total images
$('.option_image').each(function(index){
var imageDelay = index*delayTime;
$(this).delay(imageDelay).animate({ 'backgroundPositionY' : '0px', 'opacity' : '1'},fadeTime,function(){
$('.option_title').css('opacity','1');
if (index == imageLen){
//If all have been iterated through - perform this function
}
})
});
But what I want to do is:
$('.option_image').animate({ 'backgroundPositionY' : '0px', 'opacity' : '1'},fadeTime,function(){
if (index == imageLen){
$('.option_title').css('opacity','1');
$('#back').fadeTo(fadeTime,1);
$('#restart').fadeTo(fadeTime,1);
}
});
which obviously doesn't work because "index" is not defined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用延迟对象:
这需要 jQuery 1.6+
Try using a deferred object:
This requires jQuery 1.6+
您可以使用闭包,如下所示:
You could use a closure, like this:
jQuery 动画被添加到队列中。您可以在完整回调中测试队列的大小。我认为这会起作用:
jQuery animations are added to a queue. You could test against the size of the queue in your complete callback. I think this'll work: