tags:

views:

47

answers:

3

I have a page that shows a bunch of thumbnails (around 30), and the client is wanting them to appear one by one, in order, going from left to right on the page. This is what I've tried:

var start_opacity = 500;
$j('.grid-photo').each(function(i) {
    start_opacity = start_opacity + 500;            

    setTimeout(function() {
        $j(i).animate({opacity: 1}, 4000);
    }, start_opacity);  
});

It doesn't seem to know what i is referencing. Any thoughts?

+2  A: 

The .each() function passes the index and the element as the arguments of the function. It also calls the function within the context of the element (this points to your element)

You can save a quick variable var $photo = $j(this); inside your .each() and also, you can calculate your setTimeout delay by just taking 500*(i+1). Leaving us with:

$j('.grid-photo').each(function(i) {
    var $photo = $j(this);
    setTimeout(function() {
        $photo.animate({opacity: 1}, 4000);
    }, 500*(i+1));  
});
gnarf
This worked brilliantly. Thanks!
Sean Johnson
A: 

Here is a jquery plugin that you can use to achieve your purpose:

Vishal Seth
-1 - As much as I love jQuery plugins, this plugin does not solve the OP's problem. No opacity fades, no "timed revealing", and it would also over complicate the issue, as the solution the OP attempted was only 2 lines of code away from functional.
gnarf
A: 

Looks like a scope issue.

Try:

$j('.grid-photo').each(function(i) {
    start_opacity = start_opacity + 500;            
    var thisImg = $(this);
    setTimeout(function() {
        thisImg.animate({opacity: 1}, 4000);
    }, start_opacity);
});
Sean O