views:

76

answers:

4

I am currently using the code below to load images but I want to show some kind of loading gif before the image loads completely.

$('#addimage').attr('src', src[i]);
$('#addimage').show();

Since I am using this in a animated mediabox the image loading in blocks does not look good so by the time the image is loading I want to replace it by showing a loading gif. Thanks

A: 

Why not just set the src attribute to point to a loading image (EG, animated gif) and just show it the whole time?

Justin Ethier
+3  A: 

You can bind the onload event to the image.

Luca Matteis
+4  A: 

You can use the callback event for once the image has loaded. Something like:

$('#addimage').load(function() { $(this).show() });
$('#addimage').attr('src', src[i]);

So you setup the load handler first, then apply your src attribute. This is assuming the image is hidden by default (via CSS, etc).

wows
+8  A: 

Use the load() event:

$("#addimage").load(function() {
  $(this).show();
});

Edit: to show one image until another loads is a little more convoluted but entirely possible. See Image Loading.

cletus
How am I suppose to show a gif till the image is loading?
halocursed
@halocursed: added a link to what you want.
cletus