tags:

views:

3396

answers:

2

I have a page with 10 images in and I want to fade them in one after another once the image has been downloaded. how can I detect the image is loaded and ready to be displayed? and should I loop through the loaded images fadeIn and once fadedIn wait for the next to load?

A: 

You can preload the images maybe (See here) and then call the the fadein and fadeout methods sequentially in a loop.

maxpower47
got the preload part working but i dont want to fade the image in until I know its been preloaded?
monkeylee
You can use the onLoad event of the image object you just loaded like so: objImage.onLoad=imagesLoaded();. The imagesLoaded function is called after the image is loaded. Put your fadein fadeout stuff in there.
maxpower47
+1  A: 

Just use the load() event on an image. E.g.

$('#some_image').hide()
    .load(function () {
      $(this).fadeIn();
    })
    .attr('src', 'images/headshot.jpg')
Kris Erickson
so will the load function only execute when the image has fully downloaded?
monkeylee
Yes, it happens when the image is loaded.
Kris Erickson
You can see it in action here: http://jsbin.com/ejesu/edit (for as long as jsbin keeps the snippet).
Kris Erickson