-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (61 loc) · 1.81 KB
/
script.js
File metadata and controls
71 lines (61 loc) · 1.81 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
let currentIndex = 0;
let carouselInterval;
let totalImages;
function updateCarousel(animate = true) {
const carouselImages = document.querySelector('.carousel-images');
if (animate) {
carouselImages.style.transition = 'transform 0.5s ease';
} else {
carouselImages.style.transition = 'none';
}
const offset = -currentIndex * 100;
carouselImages.style.transform = `translateX(${offset}%)`;
}
function prevImage() {
if (currentIndex === 0) {
currentIndex = totalImages - 1;
updateCarousel(false); // jump to last real image instantly
requestAnimationFrame(() => {
currentIndex--;
updateCarousel();
});
} else {
currentIndex--;
updateCarousel();
}
resetTimer();
}
function nextImage() {
currentIndex++;
updateCarousel();
if (currentIndex === totalImages) {
setTimeout(() => {
currentIndex = 0;
updateCarousel(false); // reset without animation
}, 500); // match animation duration
}
resetTimer();
}
function startCarouselTimer() {
carouselInterval = setInterval(() => {
nextImage();
}, 3000); // change image every 3 seconds
}
function resetTimer() {
clearInterval(carouselInterval);
startCarouselTimer();
}
document.addEventListener("DOMContentLoaded", () => {
const carouselImages = document.querySelector('.carousel-images');
const images = document.querySelectorAll('.carousel-image');
totalImages = images.length;
// Clone the first image and append to the end
const firstClone = images[0].cloneNode(true);
carouselImages.appendChild(firstClone);
updateCarousel(false);
startCarouselTimer();
// Pause on hover
const carousel = document.querySelector('.carousel');
carousel.addEventListener('mouseenter', () => clearInterval(carouselInterval));
carousel.addEventListener('mouseleave', startCarouselTimer);
});