100-project-100-days-website/100_projects/48-create-animated-image-carousel/script.js
2023-04-10 01:41:23 +03:00
Ask

19 lines
656 B
JavaScript

{a6bb87b189609a84ee1946443d5c0d7874f1d57d true 656 script.js 0xc001da5f10}

const buttons = document.querySelectorAll("[data-carousel-button]")
buttons.forEach(button => {
button.addEventListener("click", () => {
const offset = button.dataset.carouselButton === "next" ? 1 : -1
const slides = button
.closest("[data-carousel]")
.querySelector("[data-slides]")
const activeSlide = slides.querySelector("[data-active]")
let newIndex = [...slides.children].indexOf(activeSlide) + offset
if (newIndex < 0) newIndex = slides.children.length - 1
if (newIndex >= slides.children.length) newIndex = 0
slides.children[newIndex].dataset.active = true
delete activeSlide.dataset.active
})
})