Possible Duplicate:
Official way to ask jQuery wait for all images to load before executing something
I'm writing a jQuery plugin that handles a bunch of image animations with a simple call to the plugin function (something like $("#image1").anims()
where #image1
is an image), however as part of the plugin's functionality I use $(this).width()
to get the width of the image right at the start of the function call.
But, since the plugin function is called in $(document).ready
, it's trying to get the width of the image before the image has loaded, so it's always returning 0. Is there a workaround for this?
http://docs.jquery.com/Events/load
$("#image1").load(function () {
$("#image1").anims();
});
You could preload the image
var oImage = new Image();
oImage.onload = function(){
this.width// width of loaded image
}
oImage.src = '/path/to/image.gif';
$(document).ready won't wait for images to load, it waits for the dom to be ready.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With