MooTools Image Preloading with Progress Bar
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!
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.
How can i hide the progress bar after loading the images?
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?
Is there anyway to make the bar disappear when it reaches 100% ?
how could i set this to load HTML content instead of images or just have it forward to an HTML page. sorry, kinda a JS noob, techin myself
Awesome sauce David!
Anyway, ..just a pointer for anybody who needed the let’s-get-the-progress-bar-disappear after the images done loaded.
Using the eggsact hint from David,
* use the “loader” variable’s onComplete function to place the “hide” code.*
..in here i use the simple fade to 0.
Hope it helps.
It’s very good.
I like this.
Thanks for share.
And I wrote something to introduce this project for my readers.
You can find the post about this in my website.
If something is wrong,pls figure it out.thanks.
Hi David, is there an onProgress callback for the Asset.javascript method? Thanks for this!
After taking a look at the code, onProgress is called only after an image has been successfully loaded. This doesn’t seem to be possible to be achieved with JS files. hmm…
wow….this works great…:) Well i was looking for the preloaded with progress… thanks I am trying this right now…If I have any issues i will let you know.. :)
is it anyway after 100% progress bar disappear and show’s message
i receive this error msg:
Error: $(this.options.percentageID) is null
Sourcefile: xxx/js/dwProgressBar.js
Row: 59
How to fix this ?
Hi David,
great script, but it doesn’t work with Internet Explorer 9.
Does it contain any deprecated (according to Microsoft) javascript method?
Thank you in advance!
I admit, I have not been on this webpage in a long time however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues.Great stuff as usual.
Hi David,
i found the error it was in this code
i had to cancel the + 1 after counter. i don’t know why but it went up to 120%, know it goes only to 100%.
Thank you agoin for the script.