最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Load List Items dynamically with JQuery - Stack Overflow

matteradmin11PV0评论

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').css('display', 'none') .delay(1000).fadeIn(800)

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').css('display', 'none') .delay(1000).fadeIn(800)
Share Improve this question edited Feb 8, 2011 at 12:52 Bill the Lizard 406k212 gold badges574 silver badges892 bronze badges asked Feb 8, 2011 at 11:34 satsat 1,0093 gold badges19 silver badges41 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 3

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);

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);
});

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;
});

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);
}

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);
});

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);
Post a comment

comment list (0)

  1. No comments so far