-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexscript.js
39 lines (30 loc) · 1.61 KB
/
indexscript.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
39
document.addEventListener("DOMContentLoaded", function() {
const carousel = document.querySelector(".carousel");
const totalImages = document.querySelectorAll(".carousel img").length;
const pagination = document.querySelector(".pagination");
let currentIndex = 0;
// Create pagination dots
for (let i = 0; i < totalImages; i++) {
const dot = document.createElement("button");
dot.classList.add("pagination-dot");
if (i === 0) dot.classList.add("active");
pagination.appendChild(dot);
}
const dots = document.querySelectorAll(".pagination-dot");
function slideCarousel() {
currentIndex = (currentIndex + 1) % totalImages;
carousel.style.transform = `translateX(-${currentIndex * 100 / totalImages}%)`;
updatePagination();
}
function updatePagination() {
dots.forEach(dot => dot.classList.remove("active"));
dots[currentIndex].classList.add("active");
}
document.getElementById("next").addEventListener("click", slideCarousel);
document.getElementById("prev").addEventListener("click", function() {
currentIndex = (currentIndex - 1 + totalImages) % totalImages;
carousel.style.transform = `translateX(-${currentIndex * 100 / totalImages}%)`;
updatePagination();
});
setInterval(slideCarousel, 2000); // Change image every 2 seconds
});