这将在您需要的最后一个元素上停止
(否则删除if
并使用.eq(++C % N)
!:)
$('.comment:gt(0)').hide(); // hide all but first one
var N = $('.comment').length; // count comments
var C = 0; // just a useful counter
function loop(){
if(++C < N){ // this will prevent the loop to run infinitely
$('.comment').delay(1000).fadeTo(400,0).eq(C).fadeTo(400,1,loop);
}
}
loop();
循环解释:
$('.comment') // all comments...
.delay(1000) // wait for some time and...
.fadeTo(400,0) // fadeTo 0
.eq(C) // just, the one which index equals to preIncremented C
.fadeTo(400,1,loop) // fade him to 1 and callback the LOOP !
;
编辑
在OP给我留下评论后:
jsBin 演示
var N = $('.comment').length;
var C = -1;
function loop(){
if(++C < N)
$('.comment').eq(C).delay(1000).slideUp(400, loop);
}
loop();