Mar
23
2010

Create a Quick MooTools Slideshow with Preloading Images

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; }
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code

1#slideshow-holder { width:600px; height:400px; background:url(spinner.gif) center center no-repeat #fff; position:relative; }
2#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();
		}
	});
});
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code

1window.addEvent(‘domready’,function() {
2 /* preloading */
3 var imagesDir = ‘epics/’;
4 var images = ['2.jpg','3.jpg','1.jpg','4.jpg','5.jpg'];
5 var holder = $(‘slideshow-holder’);
6 images.each(function(img,i){ images[i] = imagesDir + ” + img; }); //add dir to images
7 var progressTemplate = ‘Loading image {x} of ‘ + images.length;
8 var updateProgress = function(num) {
9 progress.set(‘text’,progressTemplate.replace(‘{x}’,num));
10 };
11 var progress = $(‘progress’);
12 updateProgress(‘text’,’0′);
13 var loader = new Asset.images(images, {
14 onProgress: function(c,index) {
15 updateProgress(‘text’,index + 1);
16 },
17 onComplete: function() {
18 var slides = [];
19 /* put images into page */
20 images.each(function(im) {
21 slides.push(new Element(‘img’,{
22 src:im,
23 width: 600,
24 height: 400,
25 styles: {
26 opacity:0,
27 top:0,
28 left:0,
29 position:’absolute’,
30 ‘z-index’: 10
31 }
32 }).inject(holder));
33 holder.setStyle(‘background’,'url(logo.png) center 80px no-repeat’);
34 });
35 var showInterval = 5000;
36 var index = 0;
37 progress.set(‘text’,'Images loaded. MooTools FTW.’);
38 (function() {slides[index].tween(‘opacity’,1); }).delay(1000);
39 var start = function() {
40 (function() {
41 holder.setStyle(‘background’,”);
42 slides[index].fade(0);
43 ++index;
44 index = (slides[index] ? index : 0);
45 slides[index].fade(1);
46 }).periodical(showInterval);
47 };
48
49 /* start the show */
50 start();
51 }
52 });
53});

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!

Leave a comment