-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
37 lines (30 loc) · 1.32 KB
/
script.js
File metadata and controls
37 lines (30 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
// })
// })
buttons.forEach(button => {
button.addEventListener('click', () => {
const offset = button.dataset.carouselButton === 'next' ? 1 : -1;
const carousel = button.closest('[data-carousel]');
const slides = carousel.querySelector('[data-slides]');
if (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;
}
});
});