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:

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

  1. 15 Must See JQuery Slideshow Plugins and Tutorials

    [...] that is both easy to implement and a snap to customize.Advanced jQuery background image slideshowSimple Slideshow using Mootools / JQueryA Beautiful Apple-style Slideshow Gallery With CSS & jQuery Tags: gallery, jquery, jQuery [...]

  2. Alex

    i do it but it does not run

  3. [Java] Gallery jquery

    [...] Gallery jquery Hướng dẫn: Simple Slideshow : Using Jquery Demo: How to Create a Simple Slideshow using Mootools / JQuery Tải về: http://www.nitinh.com/static/SlideSh…how-JQuery.zip Hướng dẫn: [...]

  4. godoom

    This looks nice. But what about browser compatibility?

  5. 50 jQuery Photo Gallery Plugins | webdesign14.com

    [...] 1. How to Create a Simple Slideshow using Mootools / JQuery [...]

  6. Debraj

    Nice Job!!! Nitin :)

  7. 十个jQuery图片画廊插件推荐 – Web前端 – JavaEye新闻 | LitiB

    [...] 1.How to Create a Simple Slideshow using Mootools / JQuery 当我们想要在有限的屏幕空间展示很多内容,这要使用到幻灯片。幻灯片是最佳的可以展现大量的信息的方式。在这篇文章中我将展示使用MooTools / Jquery做出简单的幻灯片的过程。 查看演示 [...]

  8. 十个jQuery图片画廊插件推荐 | 节点天空

    [...] 1. How to Create a Simple Slideshow using Mootools / JQuery 当我们想要在有限的屏幕空间展示很多内容,这要使用到幻灯片。幻灯片是最佳的可以展现大量的信息的方式。在这篇文章中我将展示使用MooTools / Jquery做出简单的幻灯片的过程。 [...]

  9. jQuery Image Gallery插件推荐

    [...] 1.How to Create a Simple Slideshow using Mootools / JQuery 当我们想要在有限的屏幕空间展示很多内容,这要使用到幻灯片。幻灯片是最佳的可以展现大量的信息的方式。在这篇文章中我将展示使用MooTools / Jquery做出简单的幻灯片的过程。 查看演示 [...]

  10. jill o'malley

    hi. I am a little new to this.
    Can you tell me what I would use for script references in the header?
    Thanks

  11. 十个jQuery图片画廊插件推荐 | iXHTML.com

    [...] 1. How to Create a Simple Slideshow using Mootools / JQuery [...]

  12. 12 Must See jQuery Slideshow (Sliders) Plugins and Tutorials | stylishwebdesigner

    [...] 9. Simple Slideshow using Mootools / JQuery [...]

  13. Web Designer

    Thank you for the consistent quality in content and sharing one of your favorite post.

  14. 33 Powerful jQuery Slideshow (Sliders) Plugins and Tutorials « Dragon Blog

    [...] you through the process of making a very simple(minimilistic) slideshow using Mootools / Jquery. Slider Details Slider Demo Download [...]

  15. Ray

    Can this be run in 2 instances on the one page?

    It’s a great slideshow but I would like to have it running 2 slideshows on the one page, both with their own image libraries. Possible?

  16. Raul Riera

    If you need a prev function :)

    function showPrev(){
    var len = $(‘#bigPic img’).length;
    var next = currentIndex > 0 ? currentIndex – 1 : 0; showImage(next);
    }

    1. Vanessa Colina

      You almost got me going nuts, mr. Riera. You used a different hash line. Fix it:

      function showPrev(){
      var len = $(‘#bigPic img’).length;
      var next = currentIndex > 0 ? currentIndex – 1 : 0;
      showImage(next);
      }

      Tested it and works.

  17. Andrew K

    Tested and works. Thanks!

    Make sure that when you copy and paste you don’t just leave it as one long line of javascript. There’s a comment in there, so it comments out the rest of the code if it’s all on one line.

  18. sleepy

    I cant get mine to work – The larger images are not displaying (the thumbnails are though) and the functions are not running. I am using it in Dreamweaver …could this be the problem.

  19. vishnupriya

    very nice article it is very useful.

  20. neeta jadhav

    I used ur code bt my script is not working.
    i copied div and ul tag in the body.
    css attribute in style tag.
    and javascript code in the script tag
    am i write?
    if i did wrong?please give me suggestion on my email id

  21. john

    why it doesn’t work on me??
    help please, .

  22. NewwdCo

    Hey thanks for this great script! Is there a simple way to pause the slideshow…I’m a newbie so I wouldn’t know how to write a stopstart type of function. Thanks for you help! Again great work.

  23. NewwdCo

    Looks like this maybe isn’t working in IE? It seems to be working fine in Firefox and Chrome, but not in IE7 or IE8. Anyone been able to get it working?

  24. NewwdCo

    Yea I figure it out! Ok your Demo obviously works in IE so I checked that source code against what you provide in the slideshow_jquery.zip file and the only difference is that the source code for the working example is written whereas in the .zip you’ve got change it to and it works!

  25. NewwdCo

    Oops that comment didn’t work because it stripped out the syntax… Change –> script type=”application/javascript” TO script type=”text/javascript” (in slideshow_jquery.zip) and it will work!

  26. Sanjay Mistry

    Hi,
    Does anyone know how to implement more than one gallery on a page please?
    Many Thanks
    Sanjay

  27. Sanjay Mistry

    Hello again!
    I understand I need to make changes to the script to have more than one gallery on a page, i would really be grateful if someone could let me know what changes I need to make to the script please.
    Many thanks
    Sanjay

  28. Around 50 Great Unique JQuery Gallery Plugins

    [...] 1. How to Create a Simple Slideshow using Mootools / JQuery [...]

  29. Raj

    Where in your code are you using JQuery ?
    Image show and Image display are being manipulated, that is all that is being done !

  30. Yoda

    I see that I can drop the opacity on the large images, but I’d like to drop opacity on the strip of thumbnail images. What’s the best way to do that?

  31. ben

    Hey awesome code im wondering how you can stop the jquery from auto cycling through the images, i can make it so each slide takes 999999 time to transfer to the next page but the first slide always changes no matter what :s

    Cheers!

  32. warawat

    Thank you for the consistent quality. :)

  33. ariel

    Hi,

    I havent tried this out yet. I’m sure I can get it to work and tweak it to the way I need for the website I’m working on.

    My only question is, how can I add a play/pause button to this slideshow?

    1. Ben

      If you use this code for the latter section:

      var myTimer;

      function startShow(){
      myTimer = setTimeout(“showNext()”, 3500);
      }
      function stopShow(){
      clearTimeout(myTimer);
      }

      $(document).ready(function() {
      startShow();
      showNext(); //loads first image
      $(‘#thumbs li’).bind(‘click’,function(e){
      var count = $(this).attr(‘rel’);
      showImage(parseInt(count)-1);
      });
      //you’re going to want to hide this element initially, until the user pauses the slide show, vice versa.
      $(‘.start’).bind(‘click’,function(){
      startShow();
      });
      $(‘.stop’).bind(‘click’,function(){
      stopShow();
      });
      });

      and then add buttons with the classes .start & .stop

  34. brad

    I love the slide show, but I only want the thumbnail controls and I don’t want it to auto play. Any idea of how I should adjust the javascript? May be a good boolean option for the future.

  35. Ben

    Great code! One question, how would I adjust the script to make the slideshow pause when a thumb li is clicked?

  36. GendeDios


    Nitin, you are fantastic. Thank you so much.

  37. 25 Useful jQuery Slideshow Tutorials « arena3000

    [...] 4. How to Create a Simple Slideshow using Mootools / JQuery [...]

  38. Nancy Marie

    This is very nice – thank you. Is the any way to set it so it does not autoplay?
    Thank you!

  39. user220

    Hi Nitin,

    Its a very nice and simple rotating banner which I was looking for. when I downloaded the script on my PC and tried to run, jquery was not working. Is this the problem from my side or the jquery file was missing? Let me know about it.
    Thanks.

  40. Hot jQuery Slide-Show Image Gallery Tutorials and Downloads

    [...] How to Create a Simple Slideshow using Mootools / jQuery [...]

  41. 十个jQuery图片画廊插件推荐 « Richard's blog

    [...] 1. How to Create a Simple Slideshow using Mootools / JQuery [...]

  42. 33 Powerful jQuery Slideshow (Sliders) Plugins and Tutorials

    [...] Slider Details Slider Demo Download Script [...]

  43. villa

    hi! thanks for this great slideshow, i have a problem!
    y cant see the bigpics when i open the webpage, and i don’t know why!! here is an example!
    http://www.eventosvillaadelina.com.ar/demo/fotos/decorado.html

    meaby is de jquery, or something about the conexion between the files! please help!!
    thanks!
    and sorry about my english! im from Argentina

  44. 十个jQuery图片画廊插件. | suliuer

    [...] 1.How to Create a Simple Slideshow using Mootools / JQuery 当我们想要在有限的屏幕空间展示很多内容,这要使用到幻灯片。幻灯片是最佳的可以展现大量的信息的方式。在这篇文章中我将展示使用MooTools / Jquery做出简单的幻灯片的过程。 [...]

  45. 15 jQuery slideshow bạn không nên bỏ qua-from nettut « PHP – language of future

    [...] Hướng dẫn - Demo - Tải về [...]

  46. 十大jQuery图片相册展示插件 - 肇庆零点科技团队博客

    [...] 2.How to Create a Simple Slideshow using Mootools / JQuery [...]

  47. Bruno

    hey how can I’m make it so when I click on it it stops the slide, please post the full code. I’m using it for videos so i would be best to stop when I click play on my video. thx

  48. 25 Useful jQuery Slideshow Tutorials « Wordpressed

    [...] 4. How to Create a Simple Slideshow using Mootools / JQuery [...]

  49. Mike

    Very nice article and I see that it works well, but my question is.. how would I implement this inside a wordpress page? without access to the head, how would I do this.. having some major issues here lol

  50. Roy

    Hi
    first of all, nice script :)

    i’m trying to click the thumbs and nothing happens (it doesn’t show the image i clicked on), also it doesn’t show pointer when i’m on the thumbs

    thank you,
    roy

Leave a Reply

Subscribe Me

Google Custom Search

Bookmarks