2024-03-14
3566
#vanilla javascript
David Herbert
103328
Mar 14, 2024 â‹… 12 min read

Build an image carousel from scratch with vanilla JavaScript

David Herbert David is a frontend developer by day and a technical writer by night who enjoys breaking down complex topics into comprehensible bits, digestible even by five-year-olds.

Recent posts:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 â‹… 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 â‹… 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 â‹… 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 â‹… 5 min read
View all posts

7 Replies to "Build an image carousel from scratch with vanilla JavaScript"

  1. Useful tutorial! Thanks for sharing. One issue I had is:

    else {
    curSlide-;
    }”

    Should be:

    else {
    curSlide–;
    }”

    Other than that, fantastic job!

  2. nice thanks for sharing
    should have two – on the prev button else statement to fix the issue.
    else {
    curside–;
    }

  3. The following JS script fixes the above issues.

    document.addEventListener(“DOMContentLoaded”, function () {
    // Select all slides
    const slides = document.querySelectorAll(“.slide”);

    // Initialize current slide counter
    let curSlide = 0;

    // Add event listeners for next and previous slide buttons
    const nextSlideBtn = document.querySelector(“.btn-next”);
    const prevSlideBtn = document.querySelector(“.btn-prev”);

    nextSlideBtn.addEventListener(“click”, () => navigateSlides(1));
    prevSlideBtn.addEventListener(“click”, () => navigateSlides(-1));

    // Function to navigate slides
    function navigateSlides(offset) {
    curSlide = (curSlide + offset + slides.length) % slides.length;

    slides.forEach((slide, index) => {
    const translateValue = (index – curSlide) * 100;
    slide.style.transform = `translateX(${translateValue}%)`;
    });
    }
    });

  4. I was able to get it to work with this code:

    const slides = document.querySelectorAll(“.slide”);

    slides.forEach((slide, indx) => {
    slide.style.transform = `translateX(${indx * 100}%)`;
    });

    let curSlide = 0;

    let maxSlide = slides.length – 1;

    const nextSlide = document.querySelector(“.btn-next”);

    nextSlide.addEventListener(“click”, function () {
    if (curSlide === maxSlide) {
    curSlide = 0;
    } else {
    curSlide ++;
    }
    slides.forEach((slide, indx) => {
    slide.style.transform = `translateX(${100 * (indx – curSlide)}%)`;
    });
    });

    const prevSlide = document.querySelector(“.btn-prev”);

    prevSlide.addEventListener(“click”, function () {
    if (curSlide === 0) {
    curSlide = maxSlide;
    } else {
    curSlide –;
    }

    slides.forEach((slide, indx) => {
    slide.style.transform = `translateX(${100 * (indx – curSlide)}%)`;
    });
    });

Leave a Reply