Simple Image Slider in Vanilla JavaScript

Simple Image Slider in Vanilla ptJavaScri
Code Snippet:Very simple image slider in pure javascript
Author: srijay
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 6,410
License: MIT
Edit Code online: View on CodePen
Read More

This Vanilla JavaScript code snippet helps you to create a simple image slider with dots navigation. It comes with fading transition effect while switching next and previous images.

How to Create Simple Image Slider in Vanilla JavaScript

1. First of all, create the HTML structure as follows:

<div class="container">
      <div id="slider" class="slider">
        <div class="slider-item active"><img src="" alt="" class="img-fluid"></div>
        <div class="slider-item"><img src="" alt="" class="img-fluid"></div>
        <div class="slider-item"><img src="" alt="" class="img-fluid"></div>
        <ul id="dots" class="list-inline dots"></ul>

2. After that, add the following CSS styles to your project:

.slider .slider-item{
      display: none;
      animation: reveal .5s ease-in-out;
    .slider {
      display: block;
    .slider .dots {
      text-align: center;
      padding: 10px;
    .slider .dots li {
      cursor: pointer;
      display: inline-block;
      background: #333;
      color: #fff;
      padding: 7px 11px;
      line-height: .5;
      border-radius: 50%;
      text-indent: -9999px;
      opacity: .5;
      -webkit-transition: opacity .2s;
      -o-transition: opacity .2s;
      transition: opacity .2s;
    .slider .dots{
      opacity: 1;
    @keyframes reveal{
        opacity: 0;
        opacity: 1;

3. Finally, add the following JavaScript code and done.

var slider = document.getElementById('slider')
var sliderItem = slider.getElementsByTagName('div');
var dots = document.getElementById('dots');
var dotsChild = document.getElementById('dots').getElementsByTagName('li');
for (i = 0; i < sliderItem.length; i++) {
  dotsChild[i].setAttribute("id", i);
  dotsChild[i].innerHTML = i;
  dotsChild[i].addEventListener("click", runSlider);
function runSlider(){
  var dnum = this.getAttribute("id");
  for (i = 0; i < sliderItem.length; i++) {

That’s all! hopefully, you have successfully integrated this image sider code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link