2011-04-20 66 views
3

我想获得一个div的所有孩子,淡出他们,然后在div中插入一些文本。我使用的淡出(回调函数),所以动画流畅:jquery问题.children()和.fadeOut()

var foo = $(this).parents('.foo').eq(0); 
foo.children().fadeOut(300,function() { 
    foo.prepend('some text'); 
}); 

问题是,淡出似乎是射击序列中的孩子们的每一个,而不是一次全部 - 有三个孩子,所以回调函数触发它们中的每一个,导致插入文本的三个实例。我可以把所有的孩子都包装在一个div中,然后淡出,但是如果可以的话,我想避免增加更多的标记。有另一种方法吗?

回答

2

试试这个代码:

var foo = $(this).parents('.foo').eq(0); 
foo.fadeOut(300,function() {//fade out foo 
    foo 
    .children().hide().end()//set display none to foo's children 
    .prepend('some text').show();//prepend text to foo and show it (but children have display none) 
}); 
+0

作品完美,感谢 – herpderp 2011-04-20 05:26:15

1

删除children()并直接拨打foo

另外,在回调...

function() { 
    if ($(this).siblings(':animated').length) { 
     return; 
    } 
    // What you need to do once only :) 
} 

jsFiddle