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 Simple Slideshow Using MooTools

34 Responses »
Christina Ricci

One excellent way to add dynamism to any website is to implement a slideshow featuring images or sliding content. Of course there are numerous slideshow plugins available but many of them can be overkill if you want to do simple slideshow without controls or events. We're going to create a simple slideshow from which we'll build on.

The HTML

<div id="slideshow-container">
	<img src="slideshow/cricci1.jpg" alt="Christina Ricci" />
	<img src="slideshow/cricci2.jpg" alt="Christina Ricci" />
	<img src="slideshow/cricci3.jpg" alt="Christina Ricci" />
	<img src="slideshow/cricci4.jpg" alt="Christina Ricci" />
	<img src="slideshow/cricci5.jpg" alt="Christina Ricci" />
</div>

The HTML is as simple as it can be -- one slideshow container DIV and the images that will be part of the show. This slideshow doesn't require that you use images -- you could just as easily use DIVs instead.

The CSS

#slideshow-container	{ width:512px; height:384px; position:relative; }
#slideshow-container img { display:block; position:absolute; top:0; left:0; z-index:1; }

It's important that the slideshow container receive a set height and width as well as its position set to relative. Slideshow items must have an absolute position with top and left set to 0.

The MooTools JavaScript

window.addEvent('domready',function() {
	/* settings */
	var showDuration = 3000;
	var container = $('slideshow-container');
	var images = container.getElements('img');
	var currentIndex = 0;
	var interval;
	/* opacity and fade */
	images.each(function(img,i){ 
		if(i > 0) {
			img.set('opacity',0);
		}
	});
	/* worker */
	var show = function() {
		images[currentIndex].fade('out');
		images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].fade('in');
	};
	/* start once the page is finished loading */
	window.addEvent('load',function(){
		interval = show.periodical(showDuration);
	});
});

The first step in the slideshow creation process is to define the show duration, grab the container and slideshow items, etc. Then we cycle through each image/slide to set its opacity to 0 if it's not in the first position. Next we create the function that fades out the current image and fades in the next image. The last step is directing the slideshow to start when the page has completed loading. Simple!

More to Come

This post will be the first of many with regard to improving upon this simple slideshow. In future posts, we'll be adding start/stop/pause controls, table of contents, and making this simple slideshow a MooTools class with loads of options. In the mean time, enjoy this simple slideshow!

Discussion

  1. December 21, 2009 @ 8:47 am

    Nice script, I’ll use that in a “class” manner. I have been using xFade (http://slayeroffice.com/code/imageCrossFade/xfade2.html) before, some experiment I found on the web a few years ago. I hadn’t the time to rewrite a Mootools version, and you did the job for me…

    Good work! ^^

  2. December 21, 2009 @ 8:56 am

    Hey David,

    just to say nice touch with the textmate/coda snippet buttons. Love your posts, keep it up!

  3. December 21, 2009 @ 9:50 am

    @Alex: You really writes touching stuff!

  4. December 21, 2009 @ 10:42 am

    I Prefer a Class for this stuff to. But props. Never use periodical but just the function again with delay.

  5. December 21, 2009 @ 10:44 am

    @René: Making the slideshow into a class will be the 3rd or fourth article of the series.

  6. December 21, 2009 @ 10:47 am

    @David Walsh: Indeed, that’s actually what I finally notice upon re-reading the article… Right below “More to Come”…
    Gee, I should read better! :D

  7. ben
    December 21, 2009 @ 12:19 pm

    @David Walsh: You need threaded comments :)

  8. December 21, 2009 @ 12:20 pm

    i did something similar some time ago http://xrado.hopto.org/post/moofader2

    greetings

  9. December 21, 2009 @ 10:33 pm

    Awesome, this is just what I need. I can’t wait for the next post to learn more.

  10. December 21, 2009 @ 11:49 pm

    Nice tut David, keep ‘em coming. I’m looking forward to the completed class…

  11. December 22, 2009 @ 1:56 am

    I can already tell this is going to be … hmm … what’s the word? Gripping comes to mind.

  12. kelvin
    December 22, 2009 @ 2:49 am

    Actually a slideshow exactly like this was my very first own created Mootools class :) still using it on alot of new projects

  13. December 22, 2009 @ 11:09 am

    Thanks for sharing your slide show. You are half my age and three times farther along – and I am happy for you.

  14. December 22, 2009 @ 11:11 am

    What I’d really like to know is how you get the banner menu to stay fixed.

  15. rasmus
    December 27, 2009 @ 9:55 pm

    You can use modulus (%) to shorten the code even more:

    images[currentIndex++].fade(‘out’);
    images[currentIndex %= images.length].fade(‘in’);

  16. December 28, 2009 @ 9:39 am

    @Rasmus: Awesome, really tricky~~

  17. dave ladner
    December 28, 2009 @ 4:19 pm

    David-

    Thanks! Exactly what I was looking for. Is there any way to set the fade time, to make the fade process longer or shorter?

  18. January 4, 2010 @ 9:16 pm

    Hi there! My jQuery, Moo tools slideshow stopped working suddenly and I have no idea why since I haven’t touched in several weeks. It just doesn’t slide anymore and the images are spread on top of my posts.Can you check it up? I don’t know where to look for help. My blog is http://pepua.blogspot.com/

  19. daryl
    January 17, 2010 @ 4:45 am

    Thanks for this article!

    Is there a way to make the fade transition last longer?

  20. January 17, 2010 @ 12:18 pm

    @Daryl: http://davidwalsh.name/create-a-simple-slideshow-iii

    It becomes a class in the last part of the series, where there’s a slideDuration option.

  21. anidexlu
    February 1, 2010 @ 8:53 am

    Thank you very much! I needed this and you made it absolutely simple.

  22. zac
    February 4, 2010 @ 7:21 pm

    I have no experience using Mootools at all. That said, am I the only one getting a strange start-up from the images? Right as the page loads each one of the images flashes befores it settles in on the first image to display. Also about 1/5 times it doesnt ever transition onto the next image. Im just wondering if Im doing something wrong.

  23. zac
    February 4, 2010 @ 7:30 pm

    In case anyone knows what I was talking about and gets the same effect I was, it can just be fixed by loading the js last at the end of the DOM. Works for me anyway.

  24. February 25, 2010 @ 8:38 pm

    David, thanks a lot. This saved me a huge amount of time. Great work.

  25. March 7, 2010 @ 7:22 am

    Thanks for this script. I’ve got it working in all browsers except for IE. In IE I don’t get any errors, but the images don’t change.

    I don’t know why it’s not working.

    http://www.theresessions.co.uk

  26. April 9, 2010 @ 3:00 am

    @Tony: The issue of images not changing in IE comes from putting window.addEvent(“load”) + the call to the show function inside the initial domready-event.

    See this example:
    http://jsfiddle.net/kHBUD/

    @Daryl: You can do
    currentImage.set(“tween”, { duration: 2000 }).tween(“opacity”, 0);

    via http://net.tutsplus.com/tutorials/javascript-ajax/make-your-mootools-code-shorter-faster-and-stronger/

    @Ryan: I haven’t read to the end of this series yet but I couldn’t spot a slideDuration option in the solutions given there(?)

    @David: Thanks for sharing this code with us!

  27. didier
    April 16, 2010 @ 1:49 am

    @David

    Hey David,
    just to say always a very nice work and thanks sharing this code with us !

    A question/idea : How do you imagine the best way to make thumnails of websites and incorporate them then into this slideshow (thumbnails like thoses you find in Chrome for example ) ?

  28. April 19, 2010 @ 7:03 am

    @Christian Your a legend! Thanks for helping sort that out. I’m not too good at using js, so would have never got it working otherwise.

  29. brian
    April 28, 2010 @ 4:38 am

    Excellent.
    I try to deploy it in my blogger site and it works in Chrome and FF.
    However, when I use IE to open the site, after I change the page, it freezes the IE. Do you have any solution for that?

    Thanks!

  30. davepic
    April 29, 2010 @ 12:16 am

    Great work!! This was very simple to implement and looks great!! Thanks!

  31. June 29, 2010 @ 3:45 pm

    I really like this basic slideshow. The only thing that I would like to change would be the fade duration. Any ideas on how to do it?

  32. conal
    July 7, 2010 @ 11:33 pm

    Hi, I have just implemented this on my site and it looks great. However I am also using a mootools accordian on another page and it stops working. I take the slideshow stuff away and it works again. I include the mootools-1.2.4-core file first.
    Any ideas?
    Thanks

  33. July 16, 2010 @ 2:01 pm

    Love the script, works great in FF however does not transition in IE8… any fixes?

  34. chris
    August 24, 2010 @ 3:22 pm

    I’m with rob – this works great in FF, but doesn’t seem to work in IE8. I was able to get it to work in IE8 … but only if I include at least 1 of the original pictures in the array. (very weird). As soon as I replace the img src links with localized pics, it breaks. But if at least one points to the Christina Ricci photos, it works fine. Any help would be appreciated!!!

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!