Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

MooTools Image Preloading with Progress Bar

31 Responses »

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!

Discussion

  1. July 16, 2008 @ 8:20 am

    Haha, this girl are great, :)
    Good tip David.

  2. July 16, 2008 @ 8:38 am

    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

  3. July 16, 2008 @ 9:42 am

    Didn’t I request an ass shot? HA.

  4. July 16, 2008 @ 11:09 am

    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.

  5. July 16, 2008 @ 6:21 pm

    @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?

  6. elvis 2k
    July 17, 2008 @ 2:02 pm

    It’d be cool to have the progress bar discreetly disappear when it reached 100%.

  7. July 18, 2008 @ 8:16 am

    But… how do you preload the preload images?

  8. July 18, 2008 @ 8:18 am

    @Sean: The preloading code starts on line 15 with Asset.images();

  9. July 18, 2008 @ 8:38 am

    It was a joke… but… what if the preload images are not loaded (for some unknown reason), does it still look ok without them?

  10. July 18, 2008 @ 5:34 pm

    @SeanJA: There’s only one way to find out! Try it.

  11. dan
    July 19, 2008 @ 4:37 am

    Hey Dave,
    is there a convenient way to inject the values of the array

    var images()

    vith Ajax?

    Cheers Dan

  12. July 19, 2008 @ 8:28 am

    @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.

  13. deto15
    August 18, 2008 @ 8:32 am

    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 ^^

  14. October 9, 2008 @ 4:52 pm

    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!

  15. birke
    November 3, 2008 @ 1:38 pm

    Very cool David!

    Is it possible to hide the progress bar after the images are fully loaded? How can this be done?

  16. November 3, 2008 @ 1:56 pm

    @Birke: Definitely — you’d use the “loader” variable’s onComplete function to place the “hide” code.

  17. birke
    November 3, 2008 @ 2:13 pm

    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();
    }

  18. jonathan
    March 3, 2009 @ 9:57 pm

    How can i hide the progress bar after loading the images?

  19. manolis
    March 15, 2009 @ 4:29 pm

    Hi,

    Would it possible to use it as a preloader for other objects (like swf) , and how?

    Thanks in advance.

  20. guillem
    May 15, 2009 @ 4:35 am

    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

  21. gui
    June 5, 2009 @ 3:29 am

    what if you want to show the preloader for each image, when loaded that one, show preloader for the next and so on

  22. June 5, 2009 @ 8:30 am

    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.

  23. f3
    June 15, 2009 @ 11:03 am

    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!

  24. f3
    June 15, 2009 @ 11:34 am

    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! :)

  25. sania
    August 5, 2009 @ 7:32 am

    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.

  26. January 18, 2010 @ 9:14 am

    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.

  27. January 18, 2010 @ 5:56 pm

    How can i remove the progress bar after it load the images?

  28. thomas
    February 28, 2010 @ 10:24 am

    Is there anyway to make the bar disappear when it reaches 100% ?

  29. harrison
    April 14, 2010 @ 9:50 pm

    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

  30. May 3, 2010 @ 2:55 am

    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.


    onComplete: function() {
    images.each(function(im) {
    new Element('img',{ src:im, width:200, style:'width:200px;margin:20px 20px 20px 0;' }).inject($('images-holder'));
    }), // change this part into [,] so we can continue to our "hide" code
    $('progress-bar').fade(0); // let's fade it to 0 opacity, shall we..
    }

    Hope it helps.

  31. June 9, 2010 @ 9:45 pm

    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.

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!