1

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'));
});
Community
  • 1
  • 1
t q
  • 4,593
  • 8
  • 56
  • 91

2 Answers2

4

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);
Kevin B
  • 94,570
  • 16
  • 163
  • 180
0
$('.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'));
});
  • anchor tags don't natively trigger a load event, and load event's don't bubble in all browsers. – Kevin B Oct 17 '12 at 17:28