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. display: block;
  3. width: 100%;
  4. height: auto;
  5. }
  6. p{
  7. background:none;
  8. color:#ffffff;
  9. }
  10. #slideShow #slideShowWindow {
  11. width: 650px;
  12. height: 450px;
  13. margin: 0;
  14. padding: 0;
  15. position: relative;
  16. overflow:hidden;
  17. margin-left: auto;
  18. margin-right:auto;
  19. }
  20. #slideShow #slideShowWindow .slide {
  21. margin: 0;
  22. padding: 0;
  23. width: 650px;
  24. height: 450px;
  25. float: left;
  26. position: relative;
  27. margin-left:auto;
  28. margin-right: auto;
  29. }
  30. #slideshow #slideshowWindow .slide, .slideText {
  31. position:absolute;
  32. bottom:18px;
  33. left:0;
  34. width:100%;
  35. height:auto;
  36. margin:0;
  37. padding:0;
  38. color:#ffffff;
  39. font-family:Myriad Pro, Arial, Helvetica, sans-serif;
  40. }
  41. .slideText {
  42. background: rgba(128, 128, 128, 0.49);
  43. }
  44. #slideshow #slideshowWindow .slide .slideText h2,
  45. #slideshow #slideshowWindow .slide .slideText p {
  46. margin:10px;
  47. padding:15px;
  48. }
  49. .slideNav {
  50. display: block;
  51. text-indent: -10000px;
  52. position: absolute;
  53. cursor: pointer;
  54. }
  55. #leftNav {
  56. left: 0;
  57. bottom: 0;
  58. width: 48px;
  59. height: 48px;
  60. background-image: url("../Images/plus_add_minus.png");
  61. background-repeat: no-repeat;
  62. z-index: 10;
  63. }
  64. #rightNav {
  65. right: 0;
  66. bottom: 0;
  67. width: 48px;
  68. height: 48px;
  69. background-image: url("../Images/plus_add_green.png");
  70. background-repeat: no-repeat;
  71. z-index: 10; }
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. var currentPosition = 0;
  3. var slideWidth = 650;
  4. var slides = $('.slide');
  5. var numberOfSlides = slides.length;
  6. var slideShowInterval;
  7. var speed = 3000;
  8. //Assign a timer, so it will run periodically
  9. slideShowInterval = setInterval(changePosition, speed);
  10. slides.wrapAll('<div id="slidesHolder"></div>');
  11. slides.css({ 'float': 'left' });
  12. //set #slidesHolder width equal to the total width of all the slides
  13. $('#slidesHolder').css('width', slideWidth * numberOfSlides);
  14. $('#slideShowWindow')
  15. .prepend('<span class="slideNav" id="leftNav">Move Left</span>')
  16. .append('<span class="slideNav" id="rightNav">Move Right</span>');
  17. manageNav(currentPosition);
  18. //tell the buttons what to do when clicked
  19. $('.slideNav').bind('click', function () {
  20. //determine new position
  21. currentPosition = ($(this).attr('id') === 'rightNav')
  22. ? currentPosition + 1 : currentPosition - 1;
  23. //hide/show controls
  24. manageNav(currentPosition);
  25. clearInterval(slideShowInterval);
  26. slideShowInterval = setInterval(changePosition, speed);
  27. moveSlide();
  28. });
  29. function manageNav(position) {
  30. //hide left arrow if position is first slide
  31. if (position === 0) {
  32. $('#leftNav').hide();
  33. }
  34. else {
  35. $('#leftNav').show();
  36. }
  37. //hide right arrow is slide position is last slide
  38. if (position === numberOfSlides - 1) {
  39. $('#rightNav').hide();
  40. }
  41. else {
  42. $('#rightNav').show();
  43. }
  44. }
  45. //changePosition: this is called when the slide is moved by the timer and NOT when the next or previous buttons are clicked
  46. function changePosition() {
  47. if (currentPosition === numberOfSlides - 1) {
  48. currentPosition = 0;
  49. manageNav(currentPosition);
  50. } else {

  51. currentPosition++;
  52. manageNav(currentPosition);
  53. }
  54. moveSlide();
  55. }
  56. //moveSlide: this function moves the slide
  57. function moveSlide() {
  58. $('#slidesHolder').animate({ 'marginLeft': slideWidth * (-currentPosition) });
  59. }
  60. });
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: