Lightweight Image Slider in Vanilla JS

Lightweight Image Slider in Vanilla JS
Code Snippet:Image Slider
Author: masterneme
Published: March 3, 2024
Last Updated: March 4, 2024
Downloads: 968
License: MIT
Edit Code online: View on CodePen
Read More

This code creates a lightweight image slider using Vanilla JS. It enables easy image navigation. The method manipulates image positions for a slideshow effect. It’s helpful for displaying multiple images in a compact slider format.

You can use this code on any website for a simple image slider. It’s easy to implement without external libraries, keeping your site lightweight. It offers a clean way to showcase multiple images with smooth navigation.

How to Create a Lightweight Image Slider in Vanilla JS

1. First of all, load the Font Awesome CSS (for arrow icons) by adding the following CDN link to the head tag of your webpage.

<link rel="stylesheet" href="" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />

2. Now, add the necessary HTML elements to your webpage. Include a container to hold the images and navigation buttons.

<div class="slider-container">
  <div class="image-container">
    <img src="" alt="image">
    <img src="" alt="image">
    <img src="" alt="image">
    <img src="" alt="image">
    <img src="" alt="image">
  <i class="fas fa-angle-double-left btn prev"></i>
  <i class="fas fa-angle-double-right btn next"></i>

3. Apply the following CSS styles to create the slider layout, adjust sizes, and handle transitions.

body {
    margin: 0;
    display: flex;
    justify-content: center;
    height: 100vh;
    align-items: center;
    background-color: wheat;

.slider-container {
    position: relative;
    width: 500px;
    height: 300px;
    overflow: hidden;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);

.image-container {
    display: flex;
    transition: transform 0.4s ease-in-out;

.btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    color: white;
    font-size: 20px;
    opacity: 0.5;
    cursor: pointer;

.btn.prev {
    left: 10px;
} {
    right: 10px;

.btn:hover {
    opacity: 1;

4. Finally, add the JavaScript code to enable image navigation and slideshow functionality.

const prevEl = document.querySelector(".prev");
const nextEl = document.querySelector(".next");
const imgEls = document.querySelectorAll("img");
const imageContainerEl = document.querySelector(".image-container");
let currentImg = 1;
let timeout;

prevEl.addEventListener("click", () => {

nextEl.addEventListener("click", () => {


function updateImg() {
    if (currentImg > imgEls.length) {
        currentImg = 1;
    } else if (currentImg < 1) {
        currentImg = imgEls.length;
    } = `translateX(-${(currentImg - 1) * 500}px)`;
    timeout = setTimeout(() => {
    }, 3000);

Feel free to customize the slider further by adjusting sizes, styles, and interval timings to suit your website’s design.

That’s all! hopefully, you have successfully created a lightweight image slider in Vanilla JavaScript. If you have any questions or suggestions, 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