Create A Simple Slideshow

Super Simple Slideshow

Today I would like to discuss with you a really simple way of creating a slideshow. Hopefully by the end of this tutorial you will have some idea of how to implement your own slide show. Feel free to use my code in your projects. If you have any comments or suggestions I would love to hear from you.

What we will use

We will use a combination of Html, CSS and JavaScript; specifically jQuery.

How it will work

The slideshow will slide through the images at a set interval and will also have back and forward buttons.

Most of the magic is in the CSS. So if you’re a CSS pro then this will be a breeze. The jQuery is used to add and remove style elements and to animate between the changes.

The slideshow works by lining the images up horizontally by using the float attribute and viewing them through a window that has the ‘overflow’ property set to ‘hidden’. The jQuery moves the slides back and forth by adjusting the left margin of the div that contains all the images.

Let’s make a slideshow

The first stage is to set up the html as follows:

  1. <div id="slideShow">  
  2.     <div id="slideShowWindow">  
  3.            <div class="slide">  
  4.              <img src=”img1.png”/>  
  5.                    <div class="slideText">  
  6.                       <h2>The Slide Title</h2>  
  7.                          <p>This is the slide text</p>  
  8.                    </div><!--</slideText> -->  
  9.             </div>  <!-- </slide> repeat as many times as needed -->    
  10.      </div><!--</slideShowWindow> -->  
  11.  </div><!--</slideshow> -->  
Next we will write the CSS, which is as follows:
  1. img {  
  2.     displayblock;  
  3.     width100%;  
  4.     heightauto;  
  5. }  
  6. p{  
  7.     background:none;  
  8.     color:#ffffff;  
  9. }  
  10. #slideShow #slideShowWindow {  
  11.     width650px;  
  12.     height450px;  
  13.     margin0;  
  14.     padding0;  
  15.     positionrelative;  
  16.     overflow:hidden;  
  17.     margin-leftauto;  
  18.     margin-right:auto;  
  19. }  
  20.   
  21. #slideShow #slideShowWindow .slide {  
  22.         margin0;  
  23.         padding0;  
  24.         width650px;  
  25.         height450px;  
  26.         floatleft;  
  27.         positionrelative;  
  28.         margin-left:auto;  
  29.         margin-rightauto;  
  30.     }  
  31.   
  32. #slideshow #slideshowWindow .slide, .slideText {  
  33.     position:absolute;  
  34.     bottom:18px;  
  35.     left:0;  
  36.     width:100%;  
  37.     height:auto;  
  38.     margin:0;  
  39.     padding:0;  
  40.     color:#ffffff;  
  41.     font-family:Myriad Pro, ArialHelveticasans-serif;  
  42. }   
  43. .slideText {  
  44.     background: rgba(1281281280.49);  
  45. }  
  46.   
  47. #slideshow #slideshowWindow .slide .slideText h2,   
  48. #slideshow #slideshowWindow .slide .slideText p {  
  49.     margin:10px;  
  50.     padding:15px;  
  51. }  
  52.   
  53. .slideNav {  
  54.     displayblock;  
  55.     text-indent-10000px;  
  56.     positionabsolute;  
  57.     cursorpointer;  
  58. }  
  59. #leftNav {  
  60.     left: 0;  
  61.     bottom: 0;  
  62.     width48px;  
  63.     height48px;  
  64.     background-imageurl("../Images/plus_add_minus.png");  
  65.     background-repeatno-repeat;  
  66.     z-index10;  
  67. }  
  68. #rightNav {  
  69.     right: 0;  
  70.     bottom: 0;  
  71.     width48px;  
  72.     height48px;  
  73.     background-imageurl("../Images/plus_add_green.png");  
  74.     background-repeatno-repeat;  
  75.     z-index10;    }  
As you can see there isn’t anything exciting or complicated about this CSS. In fact it doesn’t get more basic, but I promise that’s all that’s needed.

Next we will create the jQuery:
  1. $(document).ready(function () {  
  2.   
  3.     var currentPosition = 0;  
  4.     var slideWidth = 650;  
  5.     var slides = $('.slide');  
  6.     var numberOfSlides = slides.length;  
  7.     var slideShowInterval;  
  8.     var speed = 3000;  
  9.   
  10.     //Assign a timer, so it will run periodically  
  11.     slideShowInterval = setInterval(changePosition, speed);  
  12.   
  13.     slides.wrapAll('<div id="slidesHolder"></div>');  
  14.   
  15.     slides.css({ 'float''left' });  
  16.   
  17.     //set #slidesHolder width equal to the total width of all the slides  
  18.     $('#slidesHolder').css('width', slideWidth * numberOfSlides);  
  19.   
  20.     $('#slideShowWindow')  
  21.         .prepend('<span class="slideNav" id="leftNav">Move Left</span>')  
  22.         .append('<span class="slideNav" id="rightNav">Move Right</span>');  
  23.   
  24.     manageNav(currentPosition);  
  25.   
  26.     //tell the buttons what to do when clicked  
  27.     $('.slideNav').bind('click', function () {  
  28.   
  29.         //determine new position  
  30.         currentPosition = ($(this).attr('id') === 'rightNav')  
  31.         ? currentPosition + 1 : currentPosition - 1;  
  32.   
  33.         //hide/show controls  
  34.         manageNav(currentPosition);  
  35.         clearInterval(slideShowInterval);  
  36.         slideShowInterval = setInterval(changePosition, speed);  
  37.         moveSlide();  
  38.     });  
  39.   
  40.     function manageNav(position) {  
  41.         //hide left arrow if position is first slide  
  42.         if (position === 0) {  
  43.             $('#leftNav').hide();  
  44.         }  
  45.         else {  
  46.             $('#leftNav').show();  
  47.         }  
  48.         //hide right arrow is slide position is last slide  
  49.         if (position === numberOfSlides - 1) {  
  50.             $('#rightNav').hide();  
  51.         }  
  52.         else {  
  53.             $('#rightNav').show();  
  54.         }  
  55.     }  
  56.   
  57.   
  58.     //changePosition: this is called when the slide is moved by the timer and NOT when the next or previous buttons are clicked  
  59.     function changePosition() {  
  60.         if (currentPosition === numberOfSlides - 1) {  
  61.             currentPosition = 0;  
  62.             manageNav(currentPosition);  
  63.         } else {  

  64.             currentPosition++;  
  65.             manageNav(currentPosition);  
  66.         }  
  67.         moveSlide();  
  68.     }  
  69.   
  70.   
  71.     //moveSlide: this function moves the slide   
  72.     function moveSlide() {  
  73.         $('#slidesHolder').animate({ 'marginLeft': slideWidth * (-currentPosition) });  
  74.     }  
  75.   
  76. });  
I have commented on the jQuery code so that it is easier to understand. And there you have it, a super simple slideshow that could be built by anyone.

If you have any comments then please feel free to contact me.

Read more articles on jQuery:


Similar Articles