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:

TypeScript logo over a pink and white background.

Drizzle vs. Prisma: Which ORM is best for your project?

Compare Prisma and Drizzle ORMs to learn their differences, strengths, and weaknesses for data access and migrations.

Temitope Oyedele
Nov 21, 2024 â‹… 10 min read
Practical Implementation Of The Rule Of Least Power For Developers

Practical implementation of the Rule of Least Power for developers

It’s easy for devs to default to JavaScript to fix every problem. Let’s use the RoLP to find simpler alternatives with HTML and CSS.

Timonwa Akintokun
Nov 21, 2024 â‹… 8 min read
Rust logo over black marble background.

Handling memory leaks in Rust

Learn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.

Ukeje Goodness
Nov 20, 2024 â‹… 4 min read
Robot pretending to be a person.

Using curl-impersonate in Node.js to avoid blocks

Bypass anti-bot measures in Node.js with curl-impersonate. Learn how it mimics browsers to overcome bot detection for web scraping.

Antonello Zanini
Nov 20, 2024 â‹… 13 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