views:

73

answers:

2

I am working with a div that has any number of img elements inside it. Right now, each img has the following CSS:

#content > img {
display: none;
position: absolute;
top: 0px;
left: 0px;
}

And the images can be cycled-through with a function that shows and hides them in turn. While each image is loading, however, I'd like to display a "loading..." image. I'd like to use JavaScript/jQuery to swap the source of each incomplete image for the "loading..." image while still permitting it to load. What's a lean and mean way to do this?

+1  A: 

There's an imagesLoaded plugin that can be found here: http://gist.github.com/268257

Just show the loading image, bind to the imagesLoaded event and when it's triggered, hide the loading image.

UPDATE:

Actually, jQuery has built in methods to detect loading now: http://api.jquery.com/load-event/

Matt Williamson
Yes, I know this plugin. I guess I could execute a function every time one of the images is shown. If it's not complete, then the "loading" image gets shown (in front of the `div`). Otherwise, it isn't shown. I was thinking of switching the source of each image, but I guess there's no need.
Isaac Lubow
Don't forget we have the `hide` and `show` methods. I don't think the src way would work because you'd need to have it set to the large image in order to start the download.
Matt Williamson
@Matt - I don't quite get this - `you'd need to have it set to the large image in order to start the download`
Reigel
Let's say you have an image "/test.jpg" that you want to show. The browser won't start downloading it until you set the src of an img tag to "/test.jpg". So, if you wanted to do the swapping via the src attribute, you'd need to set the src attribute to some *other* img tag first, so you may as well just do it once.
Matt Williamson
hehe I'll introduce you to `$('<img>')`. and let's put `/test.jpg` as a source of it. `$('<img>').attr('src','/test.jpg')`. this is like `<img src="/test.jpg" />`. The only difference is that `$('<img>')` is not attached to `DOM`. cool ;)
Reigel
Is there any documentation relating to this? What a groovy trick.
Isaac Lubow
+5  A: 
$(function(){
   $('<img>').attr('src','loading.gif'); // preload the "loading" image.

   $('#content > img').each(function(){
       var original = this.src;
       var $this = $(this);
       $this.attr('src','loading.gif'); // swap to loading image.
       $('<img>').attr('src',original)// pre-load original.
              .load(function(){
                 $this.attr('src',original); // swap it back when pre-load is done. 
              });
   })
});

crazy example

Reigel
Now _there's_ an idea. What is the purpose of the `<` and `>` in the `$('<img>')` selector? Does this do something other than select the `img` elements?
Isaac Lubow
ahh... `$('img')` is _selecting_ elements with tag `IMG` while `$('<img>')` _creates_ an unattached to `DOM` `IMG` tag. ;)
Reigel
I like this one!
Matt Williamson
@Reigel, that's awesome!
Isaac Lubow