Simple Responsive Slideshow With Pure CSS JS – Free Code Download

Welcome to a tutorial on how to create a simple responsive slideshow with pure CSS and vanilla Javascript. Yes, there are a lot of crazy slideshow plugins on the Internet. But some of them require the use of third-party libraries, which adds to the undesired loading bloat – So here it is, a simple and lightweight slideshow that you can use. Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

TABLE OF CONTENTS

Download & Demo How It Works Auto Load Slides
The End

 

 

DOWNLOAD & DEMO

Firstly, here is the download link to the example code as promised.

 

SOURCE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICKSTART – HOW TO USE

slides.html
<!-- (A) LOAD LIBRARY -->
<link rel="stylesheet" href="slides.css"/>
<script src="slides.js"></script>

<!-- (B) CREATE HTML DIV -->
<div id="slidedemo"></div>

<!-- (C) ATTACH SLIDESHOW -->
<script>
window.addEventListener("DOMContentLoaded", function(){
  sslide.init({
    target: "slidedemo",
    images: [
      {src: "cat.jpg", cap: "An evil cate."},
      {src: "dove.jpg", cap: "A birb."},
      {src: "fox.jpg", cap: "Not a doge."}
    ],
    auto: 3000 // OPTIONAL, 3 sec per slide
  });
});
</script>
  1. Captain Obvious, load both slides.css and slides.js.
  2. Define a <div> to attach the slideshow to.
  3. Lastly, use sslide.init() to attach the slideshow. There are only 3 options here:
    • target ID of the <div> element that you want to attach the slideshow to.
    • images Array of images you want to show. Remember to provide both the source URL and caption.
    • auto In milliseconds, optional. Automatically rotate between the slides.

 

 

SLIDESHOW DEMO 

 

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

HOW IT WORKS

Now for those who are interested to “deep customize” the slideshow – Here is the Javascript and a quick walkthrough.

 

THE JAVASCRIPT

slides.js
var sslide = {
  /* (A) INITIALIZE SLIDESHOW */
  instances : [],
  init : function (opt) {
    // (A1) REGISTER SLIDESHOW INSTANCE
    let id = sslide.instances.length;
    sslide.instances.push(opt);
    // (A2) PRELOAD IMAGES
    let all = opt.images.length,
        loaded = 0;
    for (let i of opt.images) {
      let img = document.createElement("img");
      img.src = i.src;
      img.onload = function(){
        loaded++;
        if (loaded == all) { sslide.attach(id); }
      };
    }
  },
 
  /* (B) INITIALIZE - ATTACH CONTROLS */
  attach : function (id) {
    // (B1) CONTAINER ITSELF
    let inst = sslide.instances[id],
        sSlide = document.getElementById(inst.target);
    sSlide.classList.add("sSlide");

    // (B2) TOP HALF - IMAGES + CONTROLS
    let sTop = document.createElement("div"),
        sImg = document.createElement("div"),
        sLeft = document.createElement("div"),
        sRight = document.createElement("div");
    sTop.classList.add("sTop");
    sImg.classList.add("sImg");
    sLeft.classList.add("sLeft");
    sRight.classList.add("sRight");
    sLeft.innerHTML = "<";
    sRight.innerHTML = ">";
    sLeft.addEventListener("click", function(){
      sslide.nav(id, 0);
    });
    sRight.addEventListener("click", function(){
      sslide.nav(id, 1);
    });
    sSlide.appendChild(sTop);
    sTop.appendChild(sImg);
    sTop.appendChild(sLeft);
    sTop.appendChild(sRight);
    
    // (B3) BOTTOM HALF - CAPTION
    let sBottom = document.createElement("div");
    sBottom.classList.add("sBottom");
    sSlide.appendChild(sBottom);
    
    // (B4) READY!
    inst.current = -1;
    inst.sImg = sImg;
    inst.sBottom = sBottom;
    sslide.nav(id, 1);
    
    if (inst.auto) {
      inst.timer = setInterval(function(){
        sslide.nav(id, 1);
      }, inst.auto); 
    }
  },

  /* (C) NAVIGATION */
  nav : function (id, direction) {
    // (C1) CALCULATE NEXT SLIDE
    let inst = sslide.instances[id],
        slides = inst.images;
    if (direction) { inst.current++; }
    else { inst.current--; }
    if (inst.current < 0) { inst.current = slides.length - 1; } if (inst.current >= slides.length) { inst.current = 0; }

    // (C2) DRAW SLIDE
    let img = document.createElement("img");
    img.src = slides[inst.current].src;
    inst.sImg.innerHTML = "";
    inst.sImg.appendChild(img);
    inst.sBottom.innerHTML = slides[inst.current].cap;
  }
};

 

 

JAVASCRIPT SUMMARY

Not going to explain the script line-by-line. But in essence, 3 functions drive the entire slideshow.

  • A – sslide.init(OPTIONS) As in the above section, we call this on page load to initiate the slideshow. Basically, register the OPTIONS into sslide.instances and preload all the specified images.
  • B – sslide.attach(ID) Is called after all the images are preloaded in sslide.init()… This is something like “initiate part 2”, which generates all the necessary slideshow HTML for OPTIONS.target. Will go through more of the HTML and CSS in the next section.
  • C – sslide.nav() Lastly, this one will track, loop through OPTIONS.images, and update the HTML. Called when the user clicks on the last/next buttons.

 

THE HTML

For you guys who are wondering, here is what sslide.attach() will generate and attach.

<div id="slidedemo" class="sSlide">
  <!-- TOP HALF : IMAGE + CONTROLS -->
  <div class="sTop">
    <div class="sImg"></div>
    <div class="sLeft"></div>
    <div class="sRight"></div>
  </div>

  <!-- BOTTOM HALF : CAPTION -->
  <div class="sBottom"></div>
</div>
  • CSS class sSlide will be added to your specified target.
  • <div class="sTop"> will be used to contain:
    • <div class="sImg"> wrapper for the current image/slide.
    • <div class="sLeft"> navigate left button.
    • <div class="sRight"> navigate right button.
    • Pull the slide information from slideshow.slides.
    • Prime the timer to load the next.
  • <div class="sBottom"> simply holds the slide caption.

 

 

THE CSS

slides.css
/* [CONTAINER] */
.sSlide {
  background: #eee;
  max-width: 640px;
}

/* [CONTAINER TOP] */
.sTop {
  position: relative;
}

/* [IMAGE CONTAINER] */
.sImg {
  width: 100%;
  height: 350px;
  background: #eee;
}
.sImg img {
  width: 100%;
  height: 100%;
  object-fit: cover; /* fill, contain, cover, scale-down */
}

/* [CONTROLS] */
.sTop .sLeft, .sTop .sRight {
  position: absolute;
  top: 0;
  z-index: 9;
  height: 100%;
  box-sizing: border-box;
  padding: 150px 20px 0 20px;
  color: #fff;
  font-size: 2em;
  visibility: hidden;
  opacity: 0;
  transition: all 0.3s;
  cursor: pointer;
}
.sTop .sLeft { left: 0; }
.sTop .sRight { right: 0; }
.sTop:hover .sLeft, .sTop:hover .sRight {
  visibility: visible;
  opacity: 1;
  background: rgba(0, 0, 0, 0.5);
}

/* [BOTTOM - CAPTION] */
.sBottom {
  color: #fff;
  background: #000;
  padding: 10px;
}

Yep… Don’t think I don’t want to dive too deep into the CSS – These are practically the cosmetics of the slideshow. Feel free to change the colors and layout to fit your own theme.

 

 

AUTOLOAD SLIDES

That’s it for the simple slideshow, and here is a small extra that you may find useful.

 

USING PHP TO AUTOLOAD ALL SLIDES

Lazy to change the HTML every time? Simply add a few more lines of PHP to automatically include all the image files.

<script>
window.addEventListener("DOMContentLoaded", function(){
  sslide.init({
    target: "slidedemo",
    images: [<?php
      $dir = "gallery/";
      $images = glob($dir . "*.{jpg,jpeg,gif,png,bmp,webp}", GLOB_BRACE);
      $all = count($images);
      $last = $all - 1;
      for ($i=0; $i<$all; $i++) {
        printf('{src:"%s%s", cap:"%s"}%s',
          $dir, basename($images[$i]), basename($images[$i]),
          $i==$last ? "" : ","
        );
      }
    ?>]
});
</script>

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to create a simple slideshow for your project, and if you have anything to share with this guide, please feel free to comment below. Good luck and happy coding!

3 thoughts on “Simple Responsive Slideshow With Pure CSS JS – Free Code Download”

  1. I have just installed this, it is great and just what I needed. However when I loaded my own images they did not display, as my Canon camera saves as .JPG files (not .jpg) so I added JPG to the file types in gallery.php (line 14). Now it works, but I wonder if I should just rename all my JPG files to jpg instead.

  2. That was nice. Simple add useful. I also have added a few lines to code and I would like to share… You can contact me via my e-mail if you would like to.

Leave a Comment

Your email address will not be published. Required fields are marked *