How to Create a Simple Slideshow using Mootools / JQuery

slide_show_icon When we want to show lots of content in limited screen space, Slideshows comes to rescue. Making use of slideshows is the optimal way to present large amount of information. In this post i'll walk you through the process of making a very simple(minimilistic) slideshow using Mootools / Jquery.

 

You must have seen various types of slideshows around the web. Creating such slideshow is also very simple. With little smart code one can make it very quickly.

As it is said “A picture is worth a thousand words”, similarly I believe a demo is worth a thousand words. So lets hit the demo and try it ourselves.

Demo using Mootools

Demo using JQuery

So, Interested ??? Lets head over to its implementation.

Action Plan

  • Step 1 : HTML Code Markup
  • Step 2 : Setup CSS for the Slideshow
  • Step 3 : Javascript for getting desired effect

Step 1 : HTML code

In this step we’ll make basic markup for the slideshow. This basically consists two parts, one which shows the bigger picture and second to show list of thumbnails. Here is the necessary HTML code for these two parts.

<div id="bigPic">
    <img alt="" src="imgs/1.jpg" />
    <img alt="" src="imgs/2.jpg" />
    <img alt="" src="imgs/3.jpg" />
    <img alt="" src="imgs/4.jpg" />
    <img alt="" src="imgs/5.jpg" />
    <img alt="" src="imgs/6.jpg" />
    <img alt="" src="imgs/7.jpg" />
    <img alt="" src="imgs/8.jpg" />
    <img alt="" src="imgs/9.jpg" />
    <img alt="" src="imgs/10.jpg" />
</div>
 
<ul id="thumbs">
    <li class="active" rel="1"><img alt="" src="imgs/1_thumb.jpg" /> </li>
    <li rel="2"><img alt="" src="imgs/3_thumb.jpg" /> </li>
    <li rel="3"><img alt="" src="imgs/4_thumb.jpg" /> </li>
    <li rel="4"><img alt="" src="imgs/5_thumb.jpg" /> </li>
    <li rel="5"><img alt="" src="imgs/6_thumb.jpg" /> </li>
    <li rel="6"><img alt="" src="imgs/7_thumb.jpg" /> </li>
    <li rel="7"><img alt="" src="imgs/8_thumb.jpg" /> </li>
    <li rel="8"><img alt="" src="imgs/9_thumb.jpg" /> </li>
    <li rel="9"><img alt="" src="imgs/10_thumb.jpg" /> </li>
    <li rel="10"><img alt="" src="imgs/2_thumb.jpg" /> </li>
</ul>

Here fist div#bigPic contains all the big images in the slideshow. Whereas, the ul#thumbs contains thumbnails images.

Step 2 : Necessary CSS Code

This section shows, the necessary CSS code. CSS style used in demo for other styles are not included here.

#bigPic{
	width:940px;
	height:300px;
	padding:1px;
	border:1px solid #CCC;
	background-color:#FFF;
	margin-bottom:10px;
}
#bigPic img{
	position:absolute;
	display:none;
}
ul#thumbs li.active{
	border:2px solid #000;	
	background:#fff;
	padding:2px;
}
ul#thumbs, ul#thumbs li{
	margin:0;
	padding:0;
	list-style:none;
}
 
ul#thumbs li{
	float:left;
	margin-right:7px;
	margin-bottom:5px;
	border:1px solid #CCC;	
	padding:3px;
	cursor:pointer;
}
ul#thumbs img{
	float:left;
	width:80px;
	height:80px;
	line-height:80px;
	overflow:hidden;
	position:relative;
	z-index:1;		
}

Step 3 : Javascript Magic

The basic idea to get the effect is in following steps:

  1. Increase the z-index of current shown image
  2. Show next image to be shown just below current image.
  3. Now using fade out current image. This way next image will become visible.

Equivalent JS code used is as follows.

Using Mootools

If you use JQuery directly head on to next section with JQuery code.

var currentImage;
var currentIndex = -1;
var interval;
function showImage(index){
    if(index<$('bigPic').getElements('img').length){
        var indexImage = $$('#bigPic img')[index]
        if(currentImage){   
            if(currentImage != indexImage ){
                $(currentImage).setStyle('z-index',2);
                $clear(myTimer);
                var myFx = new Fx.Tween(currentImage, {
                    duration: 250,
                    onComplete:function(){
                        $(this).setStyles({'display':'none','z-index':1});
                        myTimer = showNext.delay(3000);
                    }.bind(currentImage)
                }).start('opacity',0);
            }
        }
        indexImage.setStyles({'display':'block', 'opacity':1});
        currentImage = indexImage;
        currentIndex = index;
        $$('#thumbs li').removeClass('active');
        $$('#thumbs li')[index].addClass('active');
    }
}
 
function showNext(){
    var len = $('bigPic').getElements('img').length;
    var next = currentIndex < (len-1) ? currentIndex + 1 : 0;
    showImage(next);
}
 
var myTimer;
window.addEvent('domready',function(){
    myTimer = showNext.delay(3000);
    showNext(); //loads first image
    $('thumbs').getElements('li').addEvent('click',function(e){
        var count = this.get('rel');
        showImage(parseInt(count)-1);
    });
});
JQuery Implementation
var currentImage;
var currentIndex = -1;
var interval;
function showImage(index){
    if(index < $('#bigPic img').length){
        var indexImage = $('#bigPic img')[index]
        if(currentImage){   
            if(currentImage != indexImage ){
                $(currentImage).css('z-index',2);
                clearTimeout(myTimer);
                $(currentImage).fadeOut(250, function() {
                    myTimer = setTimeout("showNext()", 3000);
                    $(this).css({'display':'none','z-index':1})
                });
            }
        }
        $(indexImage).css({'display':'block', 'opacity':1});
        currentImage = indexImage;
        currentIndex = index;
        $('#thumbs li').removeClass('active');
        $($('#thumbs li')[index]).addClass('active');
    }
}
 
function showNext(){
    var len = $('#bigPic img').length;
    var next = currentIndex < (len-1) ? currentIndex + 1 : 0;
    showImage(next);
}
 
var myTimer;
 
$(document).ready(function() {
    myTimer = setTimeout("showNext()", 3000);
    showNext(); //Load first image
    $('#thumbs li').bind('click',function(e){
        var count = $(this).attr('rel');
        showImage(parseInt(count)-1);
    });
});

I have tried to keep the javascript code really simple if you still feel it difficult to understand just drop a comment about it. I'll try to explain your doubts.

Download Code

JQuery Implementation : Sideshow-Jquery.zip
Mootools Implementation : Sideshow-Mootools.zip

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Other Posts which you may like:

80 responses to “How to Create a Simple Slideshow using Mootools / JQuery”

  1. Kim

    Hi,
    I can’t work out why this won’t work for me… :(
    For some reason, there’s no big image and I can’t click on the thumbs.

    Is there a reason for this?

  2. Kim

    The demo file also doesn’t seem to work in IE or Firefox.. Is there something I’m missing?

  3. Jeff

    I am also having a problem getting this to work. I downloaded it and set it all up but the large image is missing and the thumbnails are not clickable. I have looked at the source code and the only thing that seems “off” is that the HTML references jquery-1.4.1.js that doesn’t appear to exist anywhere in the download.

    Has anyone figured this problem out yet? There seem to be a lot of people reporting the issue but no one resolving it.

  4. Amber

    Great tutorial! Love it. However, is it simple to put in a code to pause it? Place a pause & Play text link?

  5. Jose

    i had problems with the code, so i did a
    little modifications to the jquery section.

    /***** The code Here ****/

    $(function(){
    var currentImage;
    var currentIndex = -1;
    var interval;
    var myTimer;
    function showImage(index){
    if(index < $('#bigPic img').length){
    var indexImage = $('#bigPic img')[index];
    if(currentImage){
    if(currentImage != indexImage ){
    $(currentImage).css('z-index',2);
    clearTimeout(myTimer);
    $(currentImage).fadeOut(250, function() {
    myTimer = setInterval( function(){ showNext(); }, 3000 );
    $(this).css({'display':'none','z-index':1})
    });
    }
    }
    $(indexImage).css({'display':'block', 'opacity':1});
    currentImage = indexImage; currentIndex = index;
    $('#thumbs li').removeClass('active');
    $($('#thumbs li')[index]).addClass('active');
    }
    }

    function showNext(){
    var len = $('#bigPic img').length;
    var next = (currentIndex < (len-1)) ? currentIndex + 1 : 0;
    showImage(next);
    }

    myTimer = setInterval( function(){ showNext(); }, 3000 );
    showNext(); //Load first image
    $('#thumbs li').bind('click',function(e){
    var count = $(this).attr('rel');
    showImage(parseInt(count)-1);
    });
    });

  6. Kent

    Great tutorial! Just wonder how can you show some text when mouse on the big photo also how can you include <a> for each big photo so that when one photo being clicked, it will lead to the corresponding page?

    Many thanks!

  7. Edna

    Hi,
    Thanks for this tutorial. I’m a newbie and have just completed my first website. I used this slideshow effect and had success with chrome, firefox and IE. However, with Safari 5.0.6, the big picture does not show up and the thumbnails are not clickable. How can I fix this?
    Thanks!

  8. Kent

    Thanks! How can you stop the transitions and just leave it as a normal gallery which the big photo will only change when clicking the thumbnail?

    Many thanks!

  9. Adrian

    Hi, great tutorial! I’m using several images of different widths, so I want to get it to center. I have this

    $(indexImage).css({‘display’:'block’, ‘margin’:'auto’});

    but it’s not working for me. Could you help me with this? Thanks

    1. nathansklero

      hi,
      i think should be something like this..
      margin: 0px auto;

      good luck.. ;)

  10. vivek

    Hi Nitin,
    Can i add left and right navigation arrow in slide show ?

  11. nathansklero

    Great Work Man… Awesome !! :D

    many thanks

  12. Tong

    I not see file “mootools-core.js”

    I want mootools-core.js very much

    (please give for me)

  13. gaanam

    It doesn’t work properly in IE. 1st it was to the left, i centered it.
    The last thumb image goes down to the next line. Fiddled with the css for sometime now. No changes.
    Anyway out?

    Thanks

  14. pak

    thanks a lot :) )

Leave a Reply

Subscribe Me

Google Custom Search

Bookmarks