jQuery Animation, Chaining, .each() and .animate() (or fadeIn() and fadeOut())

前端 未结 4 1126
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 15:20

I\'m having a bit of a trouble trying to figure this out today, i want to make 5 items inside my DOM (which is listed under the same attribute element, $(\'.elements\')) fade in

4条回答
  •  余生分开走
    2021-02-03 15:27

    (function loop() {
      $('.elements').each(function() {
        var $self = $(this);
        $self.parent().queue(function (n) {
          $self.fadeIn(2000).delay(200).fadeOut(2000, n);
        });
      }).parent().promise().done(loop);
    }());
    

    demo: http://jsfiddle.net/uWGVN/2/

    updated to have it looping without end.


    2nd update: a different, probably more readable, approach:

    (function fade(idx) {
      var $elements = $('.elements');
      $elements.eq(idx).fadeIn(2000).delay(200).fadeOut(2000, function () {
        fade(idx + 1 < $elements.length ? idx + 1 : 0);
      });
    }(0));
    

    ​demo: http://jsfiddle.net/uWGVN/3/

提交回复
热议问题