Load List Items dynamically with JQuery

后端 未结 6 1817
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 13:58

I\'d like to structure my JQuery to fade in each individual item at a time. Here\'s an example of the behavior, and here\'s the JQuery I have so far:

$(\'li\         


        
相关标签:
6条回答
  • 2021-01-14 14:28

    A slight variation on Ivans method

    $(function() {
        $('ul li:hidden').each(function(idx) {
            setTimeout(function(el) {
                el.fadeIn();
            }, idx* 1000, $(this));
        });
    });
    

    And a recursive function using fadeIn callback function instead of setTimeout

    function DisplayOneByOne(){
        $('ul li:hidden:first').fadeIn('2000',  DisplayOneByOne);
    }
    
    0 讨论(0)
  • 2021-01-14 14:29

    Here is how you do it:

    var delay = 200, t = 0;
    $("li").css('display', 'none').each(function(){
        t += delay;
        var $li = $(this);
        setTimeout(function(){
            $li.fadeIn(1900);
        },t);
    });
    
    0 讨论(0)
  • 2021-01-14 14:34

    Maybe something like this:

    var delay = 500, t = 0;
    $("li").css('display', 'none').each(function(){
      t += delay;
      var $li = $(this);
      setTimeout(function(){
        $li.fadeIn();
      },t);
    });
    
    0 讨论(0)
  • 2021-01-14 14:42

    There is another way to fade in elements after each other:

    $.fn.fadeInNext = function(delay) {
        return $(this).fadeIn(delay,function() {
            $(this).next().fadeInNext();
        });
    };
    
    $('li').hide().first().fadeInNext(1000);
    
    0 讨论(0)
  • 2021-01-14 14:48

    Loop through the li, and use setTimeout to queue the animation for that li.

    $('li').each(function () {
        var li = this;
        animateLi = function () {
            li.fadeIn(800);
        }
        setTimeout(animateLi, timeout);
        timeout += 100;
    });
    
    0 讨论(0)
  • 2021-01-14 14:54

    This probably not the best solution but it should work:

    $('li').each(function(i){
      var el = this;
      setTimeout(function(){
        $(el).fadeIn(800);
      },800*i)
    });
    

    Just for fun, a recursive version:

    function animateLi(i){
      $('li').eq(i).fadeIn(800);
      if ($('li').eq(i+1).length>0)
      {
          setTimeout(function(){animateLi(i+1)},800);
      }
    }
    animateLi(0);
    
    0 讨论(0)
提交回复
热议问题