jQuery each() with a delay

前端 未结 5 2011
执念已碎
执念已碎 2021-02-07 08:33

So, I would like an element to fade in and wait half a second, then fade the next in etc...

My code:

$(\'.comment\').each(function() {                 
          


        
5条回答
  •  甜味超标
    2021-02-07 09:06

    This function will iterate through every element in the collection (in this example $comments) and fade in all of them. Every animation will start when the previous one has finished.

    var $comments=$('.comment');
    
    (function fadeIterator($collection, index) {
        $collection.eq(index).fadeIn(1000, function () {
            fadeIterator($collection, index++);
        });
    }($comments, 0));
    

    jsFiddle Demo

提交回复
热议问题