tags:

views:

1036

answers:

4

Hey all,

I am trying to find a way to load a JSON page to display my content, which I currently have. But I am trying to fade in each element one after another? Is anyone familiar with a way to do that?

Fade in each element with a slight delay?

Thanks,

Ryan

+1  A: 

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.

Darryl Hein
+2  A: 

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.

Genericrich
Oh and you'll need jQuery to do this.
alex
+1  A: 

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.

DrJokepu
Is it possible to make fadeInNextElement not affect the array? I like your answer the most from the current list, but the side-effect in fadeInNextElement feels a bit wrong.
Dustin
A: 

Here is an example of my code, I am using the jquery framework.

Thanks,

Ryan

CODE: http://pastie.org/343896

Coughlin