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.

Create a Quick MooTools Slideshow with Preloading Images

15 Responses »
David Walsh Slideshow

I've been creating a lot of slideshow posts lately. Why, you ask? Because they help me get chicks. A quick formula for you:

var numChicks = $$('.slideshow').length; //simple!

The following code snippet will show you how to create a simple slideshow with MooTools; the script will also preload the images and feature a progress message. Why preload images? They make the slideshow more elegant and you can avoid an onLoad mess. Oh, and chicks...loads and loads of chicks.

The HTML

<div id="slideshow-holder">
	<div id="progress"></div>
</div>

Basically just two DIVs which will hold content.

The CSS

#slideshow-holder	{ width:600px; height:400px; background:url(spinner.gif) center center no-repeat #fff; position:relative; }
#progress			{ position:absolute; width:100%; text-align:center; color:#999; top:225px; }

The image holder is given set dimensions, starts with a background spinner, and its position set to relative; the images will all be positioned absolutely. The progress holder is set right below the spinner.

The MooTools JavaScript

window.addEvent('domready',function() {
	/* preloading */
	var imagesDir = 'epics/';
	var images = ['2.jpg','3.jpg','1.jpg','4.jpg','5.jpg'];
	var holder = $('slideshow-holder');
	images.each(function(img,i){ images[i] = imagesDir + '' + img; }); //add dir to images
	var progressTemplate = 'Loading image {x} of ' + images.length;
	var updateProgress = function(num) {
		progress.set('text',progressTemplate.replace('{x}',num));
	};
	var progress = $('progress');
	updateProgress('text','0');
	var loader = new Asset.images(images, {
		onProgress: function(c,index) {
			updateProgress('text',index + 1);
		},
		onComplete: function() {
			var slides = [];
			/* put images into page */
			images.each(function(im) {
				slides.push(new Element('img',{
					src:im,
					width: 600,
					height: 400,
					styles: {
						opacity:0,
						top:0,
						left:0,
						position:'absolute',
						'z-index': 10
					}
				}).inject(holder));
				holder.setStyle('background','url(logo.png) center 80px no-repeat');
			});
			var showInterval = 5000;
			var index = 0;
			progress.set('text','Images loaded.  MooTools FTW.');
			(function() {slides[index].tween('opacity',1); }).delay(1000);
			var start = function() {
				(function() {
					holder.setStyle('background','');
					slides[index].fade(0);
					++index;
					index = (slides[index] ? index : 0);
					slides[index].fade(1);
				}).periodical(showInterval);
			};

			/* start the show */
			start();
		}
	});
});

The first set of variable declarations represent basic settings for the preloader: images, preload-message-updating, etc. We pass our Asset.images instance an array of images. When each image loads, we update the status message. When every image has loaded, we remove the status message and start the slideshow. That's it!

Of course the above could be turned into the class....I'm just slightly lazy...Feel free to turn it into a class and share with everyone!

Discussion

  1. March 17, 2010 @ 9:20 am

    > var numChicks = $$(‘.slideshow’).length;

    Doesn’t that suggest they’re only interested in the length of your slideshows, rather than the number of slideshow posts you make? :P

  2. March 17, 2010 @ 9:22 am

    @Addy Osmani: Having a sizable slideshow doesn’t hurt. Maybe I should have used PHP:

    $num_chicks = get_posts(‘type’=>’slideshow’);

  3. March 17, 2010 @ 9:32 am

    As long as you remember to protect your $$(‘.slideshow’) with curly brackets I think you should be fine.

    You’d hate for some ‘child’ elements to randomly start appearing unexpectedly around your page ;)

  4. mlazz
    March 17, 2010 @ 11:01 am

    Dude, nice photos..! I mean, they are really good!
    Especially the purple one!

    Back to chicks – redirect guys to Christina Ricci posts so only chicks stay around ;)

    $num_chicks = get_posts(‘type’=>’slideshow’) + get_posts(‘post_has_photos_of’=>’christina+ricci’);

  5. March 17, 2010 @ 11:10 am

    I see that you use
    ” (function() {slides[index].tween(‘opacity’,1); }).delay(1000); ”
    this kinda syntax quite a lot.
    I mean (function(){}).xyz() type of code.

    Does it have a some real advantage, apart from making code a lil complex to read.

  6. March 17, 2010 @ 11:38 am

    @Nitin: I used that because the “.delay” method is a Function method, thus I need to wrap the “tween” directive in a function.

  7. vitor
    March 17, 2010 @ 12:06 pm

    You just wanted and excuse to show off ur engagement photos!!
    Showoff!! LOL

    Great job and nice Photos

  8. March 18, 2010 @ 8:26 am

    I would have thought that you could just tell chicks you were a mooTools guru, but slide shows work too!

  9. lars
    March 19, 2010 @ 3:46 am

    Nice one!

    Any easy way to make the slideshow randomize the pictures so that it will randomize the order on load?

  10. lars
    March 19, 2010 @ 3:47 am

    Nice one!

    Any easy way to make the slideshow randomize the pictures so that it will randomize the order on load?

  11. hamburger
    March 29, 2010 @ 9:59 am

    Hello David,
    i found an issue in your script:

    updateProgress(‘text’,’0′); should be updateProgress(’0′);

    and what kind of mo are you using.
    with my original one mootools-1.2.4-core-yc.js from the moo-side
    i get the error Asset is not defined.

  12. April 2, 2010 @ 3:23 pm

    @hamburger:
    Asset is part of mootools.more, go there http://mootools.net/more and download the js with Asset if you need this one only

    @David:
    I was doing a slideshow with gradient lightbox, and wanted to integrate some ready done mootools slideshow, so I went with this one. Did some refactoring to put it in a class and it works great! Thanks a lot.

    My slideshow: http://standupweb.net/mootools/gradientLightbox.php

  13. April 7, 2010 @ 2:44 pm

    hello, thanks for you. cool jquery

  14. gav
    May 13, 2010 @ 4:17 pm

    Hi there,

    Excellent slideshow but just wondering if anybody found a way of randomizing the images?
    Thanks Gav

  15. don
    June 15, 2010 @ 6:32 am

    hi guys…

    anyone know of a way to add images using an external xml file?

    thanks

    donb

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!