MooTools Image Preloading with Progress Bar
Written by David Walsh on Wednesday, July 16, 2008
Click here to learn what has changed to make your code framework-compatible.
The idea of image preloading has been around since the dawn of the internet. When we didn’t have all the fancy stuff we use now, we were forced to use ugly mouseover images to show dynamism. I don’t think you were declared an official programmer until you had seen Macromedia Dreamweaver’s mouseover function. MooTools image preloading requires very little code — let me show you how it’s done.
If you haven’t experienced the dwProgressBar class for MooTools, please read this article. I wont be featuring an explanation of that code in this article.
The XHTML
<div id="progress-bar"></div> <div id="images-holder"></div>
I’ve created two DIVs which will act as “holders” for the progress bar and the images.
The MooTools Javascript
window.addEvent('domready', function() {
/* progress bar */
var progressBar = new dwProgressBar({
container: $('progress-bar'),
startPercentage: 0,
speed:750,
boxID: 'box',
percentageID: 'perc',
displayID: 'text',
displayText: true
});
/* preloading */
var images = ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg','8.jpg','9.jpg','10.jpg'];
var loader = new Asset.images(images, {
onProgress: function(counter,index) {
progressBar.set((counter + 1) * (100 / images.length));
},
onComplete: function() {
images.each(function(im) {
new Element('img',{ src:im, width:200, style:'width:200px;margin:20px 20px 20px 0;' }).inject($('images-holder'));
});
}
});
});
The code to preload images is shockingly short. We provide Asset.images() an array of images (string paths to the images on the server). On every image load, we adjust the progress bar. Finally, once all images have loaded, we inject them into the image holding DIV.
Be sure to view the example featuring the ever-beautiful Christina Ricci!
Follow via RSS Epic Discussion
Be Heard!
I want to hear what you have to say! Share your comments and questions below.











Haha, this girl are great, :)
Good tip David.
david walsh, as you can see on this screenshot, using ie7 the pics are enlarged. pd. maybe is a mootools problem?
http://img247.imageshack.us/img247/1838/oo956e2tmpll4.jpg
Didn’t I request an ass shot? HA.
I’ve seen image preloading done before with only two images. The stripped bar would be a small repeatable image and then there would be a solid bar. (I guess it can be done with one.) Then the two divs would be right next to each and then one would grow and the other would shrink. Until the solid one would have a width of 0.
@Josep: IE7 must be acting odd with the images. The images are actually huge so that the progress bar wouldn’t be skipped right away. IE used to constrain those proportions. Apparently not anymore.
@Chris: How could I forget?
It’d be cool to have the progress bar discreetly disappear when it reached 100%.
But… how do you preload the preload images?
@Sean: The preloading code starts on line 15 with Asset.images();
It was a joke… but… what if the preload images are not loaded (for some unknown reason), does it still look ok without them?
@SeanJA: There’s only one way to find out! Try it.
Hey Dave,
is there a convenient way to inject the values of the array
var images()
vith Ajax?
Cheers Dan
@Dan: Good question. Here’s one possible hack:
Use ajax to build a string that looks like: ‘1.jpg’, ‘2.jpg’. Split that string using the .split() function.
Now you have your array.
That’s an awesome usage of mootools ^^
I had a problem with website preloader because I have tons of images as css backgrounds but Firefox 2 run body.onload event without downloading those… And now not only I can fix it, but have it with percentage bar :3
Big thanks for help ^^
david, great use of mootools and love your blog. I’ve played with your example, and I’ve noticed that onComplete doesn’t work properly in firefox. The preloaded image seems to load twice in FF, however it successful in IE/Opera/Chrome/Safari. Have you come across this issue before? Would love to hear a workaround if possible! TIA!
Very cool David!
Is it possible to hide the progress bar after the images are fully loaded? How can this be done?
@Birke: Definitely — you’d use the “loader” variable’s onComplete function to place the “hide” code.
Thank you very much for your fast reply. This was my first idea to use this function. I tried to do this with fx.slide from mootools demo page, but had no luck until yet to get it to work.
var myFx = new Fx.Slide(‘images-holder’, {
duration: ‘long’,
transition: Fx.Transitions.Bounce.easeOut
});
onComplete: function() {
images.each(function(im) {
new Element(‘img’,{ src:im, width:200, style:’width:200px;margin:20px 20px 20px 0;’ }).inject($(‘images-holder’));
});
fx.hide();
}
How can i hide the progress bar after loading the images?
I have some Java on my site and I really wanted to use this MooTool example to pre-load the heavy .jar files, I just got this working with image files but its the Jars that hold everything up for me, leaving a big blank space on my site.
How can I modify this code to pre-load .jar files, right now I have just pointed to the files. Sorry, not a programmer at all this end, thanks.
/* preloading */
var images = ['http://meta.projectmio.com/code/bubble_4.jar', 'http://meta.projectmio.com/code/core.jar'];
var loader = new Asset.images(images, {
onProgress: function(counter,index) {
progressBar.set((counter + 1) * (100 / images.length));
},
});
});
</script>
I have some Java on my site and I really wanted to use this MooTool example to pre-load the heavy .jar files, I just got this working with image files but its the Jars that hold everything up for me, leaving a big blank space on my site.
How can I modify this code to pre-load .jar files, right now I have just pointed to the files. Sorry, not a programmer at all this end, thanks.
/* preloading */
var images = ['http://meta.projectmio.com/code/bubble_4.jar', 'http://meta.projectmio.com/code/core.jar'];
var loader = new Asset.images(images, {
onProgress: function(counter,index) {
progressBar.set((counter + 1) * (100 / images.length));
},
});
});
Hi, how can I adapt this code to pre-load files as well as images? My website has large Java applet on its as the main feature and it take time to load, without it there is a big blank area, trying to preload the .jar files with your code doesn’t work. I would really like to be able to use your code for this, thanks.
Hi,
Would it possible to use it as a preloader for other objects (like swf) , and how?
Thanks in advance.
Is there a way to retrieve images addresses (url) already placed into a div, ask mootools not to load them the usual way, store them into an array and then, use your code?
Thanks
what if you want to show the preloader for each image, when loaded that one, show preloader for the next and so on
Gui: What value does showing the progress of each image have? Showing the progress as a whole is more important. In any case, I suppose you could use multiple progress bars.
hi there David -
this is really great.
I’m having one issue and I wonder if you’ve run into it before and/or have any advice. in Firefox and Safari, all is wonderful. In Windows IE 6, my code is never hitting the onComplete block – it stops at 96%.
I added an alert to the onProgress block that prints this debug info out:
“finished ” + index + ” out of ” + images.length + ” images”
in Firefox, images.length is 26, but in IE 6, images.length is 27, so this may be causing the problem.
this seems like more of a problem with Asset.images than dwProgressBar, but I’d be surprised if I were the only person seeing this behavior.
have you seen or heard anything about this? your advice is appreciated!
well, I fixed my own problem:
http://jszen.blogspot.com/2004/07/array-length-headaches.html
I had an extra comma following the last image in my image array.
thanks anyway! :)
hey could u plz tell me how to show some text with that 100% i mean that variable goes from 0 to 100 % so i want to display this text with it “loading images” of course without quotes.
Hi, david, one question,, if i want use this script for load data from database mysql usign php, and show Preloader while load data ? How do this.
How can i remove the progress bar after it load the images?