Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fade in each element - one after another

Tags:

jquery

ajax

load

People also ask

Which function is used to give a fading effect to an element?

The jQuery fadeIn() method is used to fade in a hidden element. Syntax: $(selector). fadeIn(speed,callback);

What is fade in and fadeOut in jQuery?

jQuery fadeIn() Method The fadeIn() method gradually changes the opacity, for selected elements, from hidden to visible (fading effect). Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: This method is often used together with the fadeOut() method.

How does jQuery fade work?

The jQuery fadeIn function is just a shortcut to the jQuery animate function. All it does it change the opacity from 0 to 1 over a period of time by incrementing the opacity value.


Let's say you have an array of span elements:

$("span").each(function(index) {
    $(this).delay(400*index).fadeIn(300);
});

(quick note: I think you need jQuery 1.4 or higher to use the .delay method)

This would basically wait a set amount of time and fade each element in. This works because you're multiplying the time to wait by the index of the element. The delays would look something like this when iterating through the array:

  • Delay 400 * 0 (no delay, just fadeIn, which is what we want for the very first element)
  • Delay 400 * 1
  • Delay 400 * 2
  • Delay 400 * 3

This makes a nice "one after the other" fadeIn effect. It could also be used with slideDown. Hope this helps!


Well, you could setup your fade functions to trigger the "next" one.

 $("div#foo").fadeIn("fast",function(){
      $("div#bar").fadeIn("fast", function(){
           // etc.
      });
   });

But a timer may be a better system, or a function that gets them all, puts them in an array, then pops them off one at a time with a delay in between, fading them in one at a time.


How about this?

jQuery.fn.fadeDelay = function() {
 delay = 0;
 return this.each(function() {
  $(this).delay(delay).fadeIn(350);
  delay += 50;
 });
};

I think you will need something like this:

var elementArray = yourAjaxRequestReturningSomethingEdibleByJQuery();
fadeInNextElement(elementArray);


function fadeInNextElement(elementArray)
{
    if (elementArray.length > 0)
    {
        var element = elementArray.pop();
        $(element).fadeIn('normal', function()
        {
             fadeInNextElement(elementArray);
        }
    }
}

Caution: I haven't tested it, but even if it does not work, you should get the idea and fix it easily.

By the way, I don't agree with using a timer. With a timer, there is nothing guaranteeing that the elements fade in one after each other, and the fading in of one element will only start if the previous one is over.

Theoretically, it should work, but there might be cases when your "chain" needs to stop for some reason, or the fading animation cannot finish on time, etc. Just use proper chaining.


Check out jQuery fadeIn() with a setTimeout() (standard JS function). You can checkout something I did on this site http://www.realstorage.ca/. I basically hide and show them so it can go in a loop.