I needed a jQuery img preload plugin in order to detect when an image had actually loaded. The scenario basically went as follows:
I had several images and content layered on top of one another and I needed to have them all visible. The problem I ran into was that as the browser loaded the images, certain images would load faster then others. Images on the lower layers sometimes would load before the upper most layers, causing some of the layers to be visible for a split second before the upper layers would appear. I needed a way to hide all but the upper most layer and then show all when I detected that the top most layer image had loaded.
Overview
The jQuery imgpreload plugin allows you to preload images before and/or after the dom is loaded.
Options
The following are the options provided by the image preloader plugin, you can change them globally or override the defaults by passing the settings object to the imgpreload method.
1 2 3 4 5 | $.fn.imgpreload.defaults = { each: null // callback invoked when each image in a group loads , all: null // callback invoked when when the entire group of images has loaded }; |
Usage
The following illustrates using the plugin to preload images after the dom has loaded. One important note, in this situation you are forcing a preload to be able to take advantage of the callback, the browser should start loading the image immediately.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $('#content img').imgpreload(function() { // this = jQuery image object selection // callback executes when all images are loaded }); $('#content img').imgpreload ({ each: function() { // this = dom image object // callback executes when each image loads }, all: function() { // this = jQuery image object selection // callback executes when all images are loaded } }); |
To preload images before the dom has loaded, for instance in the HEAD of the document, you would have to use specific image paths.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $.imgpreload('/images/a.gif',function() { // this = new image object // callback }); $.imgpreload(['/images/a.gif','/images/b.gif'],function() { // this = array of new image objects // callback executes when all images are loaded }); $.imgpreload(['/images/a.gif','/images/b.gif'], { each: function() { // this = new image object // callback executes on every image load }, all: function() { // this = array of new image objects // callback executes when all images are loaded } }); |
Download
jQuery imgpreload plugin, this project is on github
{ 16 comments… read them below or add one }
thank u. this is very useful for me.
but i think the ‘each’ part does not work correctly.
i want to use it to make progress bar but i can t.
can u help me?
Reza, I would love to help, please let me know what you’re having troubles with, I’ll review the “each” statement to confirm if its working or not (thanks for the heads up).
Wow, thank you so much! I have been looking for this plugin or the code to create it for hours.
This plugin is exactly what I was searching for however I was unable to get the “all” callback function to fire in IE7. “each” works fine, but “all” was not called.
Code was basically
$(‘#content img’).imgpreload({
each: function(){
alert(‘!’);
}.
all: function(){
alert(‘!’);
}
});
Any ideas?
Michael, I’ve tested in IE7 and all seems fine … note that if an image does not exist all will not get called, “each” for each of the images that were successful will get called …
I will add a “complete” callback which fires regardless of whether the images loaded or not.
thanks a lot. Exactly what I was looking for
I’m trying to use this inside Wordpress and I’m getting an error when I use it:
$(“#randimg img”).imgpreload is not a function
It works if I have my script and this plug-in in the body but doesn’t work if I load either or both my script and the plugin in . What am I doing wrong? Thanks.
Hi George,
Typically you will get the “is not a function” error when something is not loaded properly or when the function is being called before the plugin actually loads. Confirm by viewing your source that jquery and the plugin are loading before your script is being called.
Very nice.
Thanks..very useful..I still have a question.
What if you want to load 20 images and you dont want to specify the path one by one. Is there a way you can load images with different paths BEFORE the DOM is built?
With out the DOM being loaded, you wouldn’t be able to target the img elements to start preloading. Doing it before the DOM is loaded requires that you specify the image paths.
You can specify the image paths as an array, see the example above.
Another idea might be to build the img paths array with javascript, this might save you the headache of specifying each path one by one.
I cant believe this is not a part of jquery. why not have in built in? (a la mootools Asset class). its was a real pain to find this plugin…
nice plugin. exactly what im looking for. thanks!
Brilliant! :p ive been looking for a decent image preloader for a while now to implement on my site as im currently building a new portfolio. This fits the bill exactly, the only thing i would take into consideration is YAACH’s comment, it would be nice to also have loop function within the class so u can just specify a directory an let it take care of the rest but ill attempt to add this in myself thanx again this is awesome :p
Excellent work brother! Works a treat
Hey, you can check out a great jQuery Preloader that I wrote with full callbacks, auto reading of images to preload, and a lot of easing in animations. Check it out here: jQuery Preloader