Simple JavaScript Image Slider Code

Simple JavaScript Image Slider Code
Code Snippet:Image Carousel - Vanilla JS
Author: Natalie Birch
Published: April 18, 2024
Last Updated: April 18, 2024
Downloads: 76
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a simple image slider with next and previous buttons. It displays images and captions. It helps showcase multiple images smoothly.

You can use this code on any website to showcase images attractively. It enhances user engagement by offering an interactive slideshow experience. It’s customizable and easy to integrate into your web projects.

How to Create a Simple Image Slider Using JavaScript

1. First of all, load the Google Fonts by adding the following CDN links into the head tag of your HTML document.

<link rel="preconnect" href="">
<link href="" rel="stylesheet">

2. In HTML, create a <div> element with the class “carousel-container”. Inside this container, add another <div> with the class “carousel-imgs”. Inside the “carousel-imgs” div, add <img> tags for each image you want to include in your slider. You can also add navigation buttons and slide numbers if desired.

<div class="carousel-container">
  <div class="carousel-imgs">
  <img src="" class="carousel-img visible"/>
  <img src="" class="carousel-img  hidden"/>
  <img src="" class="carousel-img  hidden"/>
    <a class="prev arrow">&#10094;</a>
    <a class="next arrow">&#10095;</a>
  <div class="slide-numbers">
      <span class="dot active"></span>
      <span class="dot"></span>
      <span class="dot"></span>
<div class="carousel-captions">
  <p class="carousel-caption visible">Mountain Range 1</p>
  <p class="carousel-caption hidden">Mountain Range 2</p>
  <p class="carousel-caption hidden">Mountain Range 3</p>

3. After that, add CSS styling to your HTML to make your image slider visually appealing and responsive. You can customize the styling to match your website’s design preferences. Ensure that the carousel container and images are properly positioned and styled for a smooth user experience.


  margin:1em auto .5em auto;
.carousel-container, .carousel-caption{
  box-shadow: 0 0 5px rgb(204,204,204);;
.carousel-container img{

.prev, .arrow{
 cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin: -1.5em .3em 0 .3em;
  padding: 1em;
  color: white;
  font-weight: bold;
  font-size: 1.2em;
  transition: 0.3s ease;
  border-radius: .5em;
  user-select: none;
  background-color:rgba(204,204,204, 0.3);
.next {
  right:0 ;
.prev:hover, .next:hover{
  box-shadow: 0 0 5px #fff;
  border:1px solid #fff;
  text-align: center;

  cursor: pointer;
  height: 10px;
  width: 10px;
  margin: 0 2px;
  background-color: rgba(204,204,204, 0.3);
  border-radius: 50%;
  display: inline-block;

.dot:hover, .active{
  background-color: rgb(204,204,204);
  height: 12px;
  width: 12px;
   box-shadow: 0 0 5px #fff;
  text-align: center;
  font-family:'Nanum Gothic',serif;
  background-color: rgba(204,204,204,.8);
  margin:.5em auto;
  padding:1em 0;

4. Finally, create a new JavaScript file and link it to your HTML document. In the JavaScript file, define variables to select the necessary HTML elements such as images, navigation buttons, and slide numbers. Then, add event listeners to the navigation buttons to allow users to navigate between images. Implement functions to update the position of the slider when a navigation button is clicked.

// Variables
let prev = document.querySelector('.prev');
let next = document.querySelector('.next');
let imgs = document.querySelectorAll('.carousel-img');
let dots = document.querySelectorAll('.dot');
let captions = document.querySelectorAll('.carousel-caption')
let totalImgs = imgs.length;
let imgPosition = 0;

// Event Listeners
next.addEventListener('click', nextImg);
prev.addEventListener('click', prevImg);

// Update Position
function updatePosition (){
//   Images
  for(let img of imgs){
//   Dots
  for (let dot of dots) {
     dot.className = dot.className.replace(" active", "");
//   Captions
  for (let caption of captions) {

// Next Img
function nextImg(){
  if (imgPosition === totalImgs -1){
        imgPosition = 0;
    } else{
//Previous Image
function prevImg(){
  if (imgPosition === 0){
        imgPosition = totalImgs-1;
    } else{

// Dot Position
dots.forEach((dot, dotPosition) => {
  dot.addEventListener("click", () => {
    imgPosition = dotPosition

That’s all! hopefully, you have successfully integrated this JavaScript code into your project to create a simple image slider. If you have any questions or suggestions, feel free to comment below.

Leave a Comment