Creating a Simple Slideshow using jQuery

Heya folks! Many of you would have seen the facebook's implementation of photo gallery, but it lacks something like automatic playing of all the photos, or in general words, a slide show. So, lets see how we can do it in a simple way using jQuery.

We have three parts in this. One tool I would like to introduce you people is JSFiddle - Code Playground. This tool just helps us in developing interactive web content. Okay, let me say what are those three things:
  1. HTML - The markup of elements!
  2. CSS - Cascading Style Sheet for a beautiful presentation!
  3. JavaScript - Not Java, but a browser script for interaction!
First thing we need is a blank HTML Skeleton. I generally prefer you people to use XHTML 1.1 for developing websites, as it is understood fully by almost all the browsers you can name of. So, lets see how the skeleton looks like:


    
        
        
    
    
     
    
Now after this basic skeleton is done, we need something to wrap our images. Generally for all the layout stuffs, we use the DIV tag. So, lets add one DIV tag with its ending tag. The code looks this way:
We can put it inside the body, where you want the slideshow to happen. Now, after our wrapper, we need the images to be inside the slideshow. So, we have to add the IMG tags inside the DIV tag this way:
[img]images/Slide1.png[/img] [img]images/Slide2.png[/img] [img]images/Slide3.png[/img]
In your jsFiddle, you need to put the above content on the HTML section. Anyways, you can see a live demo soon. Just make sure to select jQuery latest version from the left side drop down menu.
Now comes the presentation part. Once we have done with the HTML Markup, we need to work on the CSS to make it beautiful by giving position, colour, etc. So, this stuff goes into the STYLE tags inside the HEAD part below the TITLE.
We use the tag names as is, i.e., BODY and IMG. When we add an ID attribute for an element, we refer them using a hash (#), i.e., #slides. Note, each element should have only unique IDs. Same IDs cannot be used for other elements. And, for the classes, we refer them using a dot (.), i.e., .active. Now the below stuff goes into the CSS part of the jsFiddle. Note that there's no HTML Tah
body {
  background-color: #000000;
}
#slides {
  position: relative;
  margin: 0 auto;
  width: 760px;
}
#slides img {
  position:absolute;
  top: 25px;
  left: 25px;
  display: none;
}
#slides img.active {
  display: block;
}
Now even the presentation part is over. So, ultimately the UI is ready. But we need to work on the interaction. This is the JavaScript part, where the main Slideshow's animation will be there. Even this part goes into the HEAD, after the previously added STYLE tag.
Here, what we are doing is, immediately after the page is loaded, we give a class "active" to the first image, so that it becomes visible. Then for every 5 seconds, we are calling the function slideshow(). This function, fades through each of the image and shows it one by one. The plain JavaScript to be used inside the jsFiddle's JavaScript part is below:
$(document).ready(function () {
    // Make the first image to be active.
    $("#slides img:first").addClass("active");
    // Play the slideshow for an interval of 5 seconds.
    setInterval("slideshow()", 5000);
});
function slideshow()
{
  var active = $("#slides .active");
  var next = ($("#slides .active").next().length > 0) ? $("#slides .active").next() : $("#slides img:first");
  active.fadeOut(function(){
      active.removeClass("active");
      next.fadeIn().addClass("active");
  });
}
You can check out the working jsFiddle here: Edit fiddle - JSFiddle - Code Playground
And for your convinience, the full HTML code is given below:


    
        
        
        
        
        
    
    
        
[img]images/Slide1.png[/img] [img]images/Slide2.png[/img] [img]images/Slide3.png[/img]
Hope you enjoy it!

Replies

  • Sahithi Pallavi
    Sahithi Pallavi
    Thanks. Nice one. Now How to make manual buttons at the bottom that allows the user to change the slides. I'm talking about 'Next' and 'Previous' buttons?
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    Sada
    Thanks. Nice one. Now How to make manual buttons at the bottom that allows the user to change the slides. I'm talking about 'Next' and 'Previous' buttons?
    That requires some good amount of functionality. Will guide you in that too! For next, it is simple, as you can call the slideshow() function, for Previous, we need to reverse the slidehow() function...

You are reading an archived discussion.

Related Posts

why freezar is upar most position in tha freeze??
tell me the emerging projects in instrumentation for the future for doing my final year BE project
Mass and weight may sound the same thing, but they aren't. while mass is a scalar, which only is the quantity of substance in a body, weight is actually a...
Aloke Bajpai is the co-founder & CEO of IXIGO.com - one of the hotest travel sites in India. Aloke is an amazing guy and I've had a chance to meet...
Reliance is launching another 3G tab and pricing it at Rs. 14500. Sometime around August last year, Reliance had announced their new tab (3G) for about Rs. 13000 making it...