我有一个列表,我用动画形式淡出。 完成此操作后,我想运行一些代码,但希望运行一次代码,而不是每次列表中的每个元素都完成动画处理。 一旦所有动画完成并且所有li
都被删除,代码必须运行。 这是我当前的代码:
$(".myclass li").animate({'opacity':'0', 'height':'0%'}, function(){
$(this).remove(); //This is run for every "li" which is fine...
function() { /* I want to now run this code only once AFTER all animation has finished */ }
});
$.queue() 应该可以帮助你解决这个问题。 http://api.jquery.com/queue/
$(".myclass li").animate({'opacity':'0', 'height':'0%'},{"queue":true});
$(".myclass li:last-child").queue(function() {
/* The code you want to run */
});
一种可能的解决方案是:
var elts=$$(".myclass li"),
num=elts.length,
onComplete=function() {
...
};
elts.animate({....}, function(){
$(this).remove();
if(--num === 0 && onComplete) {
onComplete();
}
});