-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarousel.js
31 lines (25 loc) · 1.11 KB
/
carousel.js
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
document.querySelectorAll(".carousel").forEach(carousel => {
const items = carousel.querySelectorAll(".carousel-item");
const buttonsHtml = Array.from(items, () => {
return `<span class="carousel-btn nes-pointer"></span>`;
});
carousel.insertAdjacentHTML("beforeend", `
<div class="carousel-nav">
${buttonsHtml.join("")}
</div>
`);
const buttons = carousel.querySelectorAll(".carousel-btn");
buttons.forEach((button, i) => {
button.addEventListener("click", () => {
//unselect all items
items.forEach(item => item.classList.remove("carousel-item-selected"));
buttons.forEach(button => button.classList.remove("carousel-btn-selected"));
items[i].classList.add("carousel-item-selected");
button.classList.add("carousel-btn-selected");
document.getElementById('target').textContent = `Week #${i + 1}`;
});
});
items[0].classList.add("carousel-item-selected");
buttons[0].classList.add("carousel-btn-selected");
document.getElementById('target').textContent = `Week #1`;
});