tags:

views:

67

answers:

3

Hello,

I have about 10 images, which I want to fade out, but delay the fadeout show that each image is shown clearly.

    for(i=1;i<=24;i++) {
 $('.stretch').fadeOut(5000).attr('src','images/hello'+i+'.jpg');
     }

I want to use jQuery.

Thanks Jean

A: 

Try this code:

for(i=1;i<=24;i++) {
        $('.stretch').fadeOut(function() { 
            $(this).load(function() { $(this).fadeIn(); }); 
            $(this).attr('src','images/hello'+i+'.jpg');
        });
}
antyrat
Works the same as my code.
Jean
You can also try to use delay function. For example: $('.stretch').delay(800).fadeOut(5000).attr('src','images/hello'+i+'.jpg');
antyrat
You are changing each images src. http://api.jquery.com/attr/
Peter Ajtai
+1  A: 

This is off the top of my head, but the basics of it are this: you'll need to do an each function, with a slightly longer pause each time. The timing of course would differ considerably, but something like this should give you an idea:

delayTime = 0;
jQuery('.stretch').each( function() {
  jQuery(this).delay(delayTime).fadeOut(5000);
  delayTime = delayTime + 5000;
});

So, again, that's an untested snippet, but the concept is simple - you're basically saying I want to fade the first one right away, then fade the second one 5000 later, then the third one 5000 after that, etc, etc.

Does that make sense?

jasonpgignac
Nice. The only caution is that this will fade each DOM element within .stretch.... so if there are things like paragraphs, lists, etc. in addition to images, those will also disappear.
Peter Ajtai
+4  A: 

Caution: attr('src', 'blah') will SET the src TO blah... I don't think that's what you want here are some alternatives:

Why don't you have all those images share a class name, then you remove the need for the for loop, and you can simply fade all the images using the class name?

$('.chosenImage', '.stretch').fadeOut(5000);

The above is equivalent to using the CSS

$('.stretch .chosenImage').fadeOut(5000);

Just be careful that you put the parent first in the CSS version, and the child first in the comma separated JQuery syntax version.

The above will go to work on all of the chosenImage class items that are children of a stretch class item.


You can use regex: This will get all helloXX where XX is a one or two digit number... you can refine the regex to only pick up 1 - 24 if you want.

$('img', '.stretch').filter(function(){
    return $(this).attr('src').match(/images\/hello[0-9]{1,2}.jpg/);
}).fadeOut(5000);

This code takes all the IMGs within the .stretch class and it filters them using a regular expression on each IMGs src attribute.


To have them fade one after another, you would just target each image one by one and put an increasing delay on them... something like this:

var delayIt = -1000;
$('img', '.stretch').each(function(){                
    delayIt += 1000;
    $(this).delay(delayIt).fadeOut(5000);
});

This will fade each image in the class stretch one after another.


Some pertinent JQuery references:

attr()

delay()

each()

fadeOut()

Peter Ajtai
I think they want to fade the images out one at a time in sequence, but perhaps I misunderstood.
jasonpgignac
@jasonpgignac Well, I added that code too, just in case.
Peter Ajtai
Yup, that's pretty much what I did, only I put the += line AFTER the delay and fade line, so that the first object would start fading immediately.
jasonpgignac
@jaonpgignac - Yes. Starting to fade immediately is nice. I edited the code and made delayIt start at -1000. Same end result, and the final value of delayIt will be the final value used, not 1000 more... Question of taste which addition method to go with I guess.
Peter Ajtai
Yup, at that point it's a matter of how you want the timing. Nice.
jasonpgignac