jQuery each()有一个延迟

Bar*_*der 14 javascript jquery

所以,我想要一个元素淡入并等待半秒钟,然后淡出下一个...

我的代码:

$('.comment').each(function() {                 
                    $(this).css({'opacity':0.0}).animate({
                        'opacity':1.0
                    }, 450).delay(500);
                });
Run Code Online (Sandbox Code Playgroud)

我显然做了一件非常愚蠢的事......(我希望)......我的问题是:这甚至可能吗?如果没有 - 有人能指出我正确的方向吗?

感谢您!

ste*_*ecb 38

或者,像这样:

$.each($('.comment'), function(i, el){

    $(el).css({'opacity':0});

    setTimeout(function(){
       $(el).animate({
        'opacity':1.0
       }, 450);
    },500 + ( i * 500 ));

});
Run Code Online (Sandbox Code Playgroud)

demo => http://jsfiddle.net/steweb/9uS56/


Ash*_*rat 12

尝试这样的事情

$(this).find('#results').children().each(function(index){
        $(this).delay(500 * index).slideDown(500);
})
Run Code Online (Sandbox Code Playgroud)

  • 很棒的答案.我试图做同样的事情,将延迟与索引相乘的技巧效果很好.谢谢! (2认同)

Mat*_*ela 5

试试这个:

var time = 500;
$('.comment').each(function() {                 
     var $this  = $(this);
    function delayed() {
         $this.css({'opacity':0.0}).animate({
                    'opacity':1.0
                }, 450);
     }
    setTimeout( delayed , time );
    time += 500;
 });
Run Code Online (Sandbox Code Playgroud)