Simple JavaScript Image Slider

Simple JavaScript Image Slider
Project: Fullscreen Image Slider
Author: Brad Traversy
Edit Online: View on CodePen
License: MIT

This code snippet helps you to create a simple image slider using Vanilla JavaScript. It creates a basic image slider with left and right arrow buttons. The slider images code defines a slider with three images and two arrow buttons. This code is to create a slideshow or carousel that allows users to navigate through the images by clicking on the left or right arrow buttons.

How to Create a Simple JavaScript Image Slider

Create the HTML structure for the image slider as follows:

<div class="wrap">
  <div id="arrow-left" class="arrow"></div>
  <div id="slider">
    <div class="slide slide1">
      <div class="slide-content">
        <span>Image One</span>
    <div class="slide slide2">
      <div class="slide-content">
        <span>Image Two</span>
    <div class="slide slide3">
      <div class="slide-content">
        <span>Image Three</span>
  <div id="arrow-right" class="arrow"></div>

Now, style the slider image using the following CSS styles:

.slide-content {
  margin: 0;
  padding: 0;
  font-family: Arial, Helvetica, sans-serif;
  width: 100%;
  height: 100vh;
  overflow-x: hidden;

.wrap {
  position: relative;

.slide {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;

.slide1 {
  background-image: url("");
.slide2 {
  background-image: url("");
.slide3 {
  background-image: url("");

.slide-content {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;

.slide-content span {
  font-size: 5rem;
  color: #fff;

.arrow {
  cursor: pointer;
  position: absolute;
  top: 50%;
  margin-top: -35px;
  width: 0;
  height: 0;
  border-style: solid;

#arrow-left {
  border-width: 30px 40px 30px 0;
  border-color: transparent #fff transparent transparent;
  left: 0;
  margin-left: 30px;

#arrow-right {
  border-width: 30px 0 30px 40px;
  border-color: transparent transparent transparent #fff;
  right: 0;
  margin-right: 30px;

Finally, add the following JavaScript function for its functionality:

let sliderImages = document.querySelectorAll(".slide"),
  arrowLeft = document.querySelector("#arrow-left"),
  arrowRight = document.querySelector("#arrow-right"),
  current = 0;

// Clear all images
function reset() {
  for (let i = 0; i < sliderImages.length; i++) {
    sliderImages[i].style.display = "none";

// Init slider
function startSlide() {
  sliderImages[0].style.display = "block";

// Show prev
function slideLeft() {
  sliderImages[current - 1].style.display = "block";

// Show next
function slideRight() {
  sliderImages[current + 1].style.display = "block";

// Left arrow click
arrowLeft.addEventListener("click", function() {
  if (current === 0) {
    current = sliderImages.length;

// Right arrow click
arrowRight.addEventListener("click", function() {
  if (current === sliderImages.length - 1) {
    current = -1;


That’s all! hopefully, you have successfully created image sliders. If you have any questions or suggestions, feel free to comment below.

Leave a Comment


No comments yet. Why don’t you start the discussion?

Leave a Reply