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:

Comparing Mutative Vs Immer Vs Reducers For Data Handling In React

Comparing React state tools: Mutative vs. Immer vs. reducers

Mutative processes data with better performance than both Immer and native reducers. Let’s compare these data handling options in React.

Rashedul Alam
Apr 26, 2024 â‹… 7 min read
Radix Ui Adoption Guide Overview Examples And Alternatives

Radix UI adoption guide: Overview, examples, and alternatives

Radix UI is quickly rising in popularity and has become an excellent go-to solution for building modern design systems and websites.

Nefe Emadamerho-Atori
Apr 25, 2024 â‹… 11 min read
Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 â‹… 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 â‹… 6 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