s_p
s_p

Reputation: 4693

jQuery each index iteration

I have a wall of images and underneath some text, with that i would like the image to fade in then its text to fade in.
With this code below, each image fades in then ALL of the text fades in at once.

Q: how can i use index so that the corresponding text will fade in following its image?
I've found a similar question but could'nt get this to work.

css:
.mydiv a, .mydiv p{ display:none;}

js:

$('.mydiv a').each(function(index){
    var c = $(this);
    $(new Image()).load(function(){
            c.fadeIn(500);
            setTimeout(function(){ $('.mydiv p').fadeIn(250); }, 500);// where would i place index
    }).attr('src', c.find('img').attr('src'));
});

Upvotes: 1

Views: 245

Answers (2)

user1744166
user1744166

Reputation:

$('.mydiv a').each(function(i, e){
    var c = $(this);
    e.load(function(){
            c.fadeIn(500);
            setTimeout(function(){ $('.mydiv p').fadeIn(250); }, 500);// where would i place index
    }).attr('src', c.find('img').attr('src'));
});

Upvotes: 0

Kevin B
Kevin B

Reputation: 95023

Navigate from the existing c variable if there is one mydiv for each p element

$(c).closest(".mydiv").find("p").fadeIn(250);

or use .eq()

$(".mydiv p").eq(i).fadeIn(250);

Upvotes: 4

Related Questions