-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
38 lines (32 loc) · 1.21 KB
/
main.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
32
33
34
35
36
37
38
document.querySelectorAll(".carousel").forEach((carousel) => {
const items = carousel.querySelectorAll(".carousel__item");
const buttonsHtml = Array.from(items, () => {
return `<span class="carousel__button"></span>`;
});
carousel.insertAdjacentHTML(
"beforeend",
`
<div class="carousel__nav">
${buttonsHtml.join("")}
</div>`
);
const buttons = carousel.querySelectorAll(".carousel__button");
buttons.forEach((button, i) => {
button.addEventListener("click", () => {
// Unselect all the items
items.forEach((item) =>
item.classList.remove("carousel__item--selected")
);
buttons.forEach((button) =>
button.classList.remove("carousel__button--selected")
);
// Adding when selecting
items[i].classList.add("carousel__item--selected");
button.classList.add("carousel__button--selected");
});
});
// By default we are making item 1 to be visible by adding the selected class to items[0],buttons[0]
items[0].classList.add("carousel__item--selected");
buttons[0].classList.add("carousel__button--selected");
console.log(buttonsHtml);
});