forked from RishabhMakes/saurabh-epk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (98 loc) · 3.75 KB
/
script.js
File metadata and controls
114 lines (98 loc) · 3.75 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
document.addEventListener('DOMContentLoaded', init);
function init() {
const carousel = document.querySelector('.carousel');
if (!carousel) return;
const track = carousel.querySelector('.carousel-track');
if (!track) return;
let visibleCount = getVisibleCount();
let autoTimer = null;
let slideWidth = 0;
let currentIndex = 0; // Will be set after cloning
// Clean previous clones if re-initialized
track.querySelectorAll('.clone').forEach(n => n.remove());
const originalSlides = Array.from(track.querySelectorAll('.slide'));
const originalCount = originalSlides.length;
if (originalCount === 0) return;
// Function to determine slides to show based on window width
function getVisibleCount() {
if (window.innerWidth <= 768) {
return 2; // 2 slides for mobile
}
if (window.innerWidth <= 992) {
return 2; // 2 slides for tablet
}
return 3; // 3 slides for desktop
}
// Clone last visibleCount slides to the start (in reverse order), and first visibleCount to the end
for (let i = 0; i < visibleCount; i++) {
const cloneStart = originalSlides[originalCount - 1 - i].cloneNode(true);
cloneStart.classList.add('clone');
track.insertBefore(cloneStart, track.firstChild);
}
for (let i = 0; i < visibleCount; i++) {
const cloneEnd = originalSlides[i].cloneNode(true);
cloneEnd.classList.add('clone');
track.appendChild(cloneEnd);
}
const allSlides = Array.from(track.children);
currentIndex = visibleCount; // Start at the first real slide
// Set sizes
function setSlideSizes() {
// Recalculate visible count on resize
visibleCount = getVisibleCount();
slideWidth = carousel.clientWidth / visibleCount;
allSlides.forEach(s => {
s.style.width = `${slideWidth}px`;
});
// Position at the currentIndex without transition
track.style.transition = 'none';
updatePosition();
// Force reflow then restore transition
void track.offsetWidth;
track.style.transition = 'transform 0.5s ease';
}
function updatePosition() {
track.style.transform = `translateX(-${currentIndex * slideWidth}px)`;
}
function nextSlide() {
currentIndex++;
track.style.transition = 'transform 0.5s ease';
updatePosition();
}
// Handle loop reset after transition when we moved into cloned area
track.addEventListener('transitionend', () => {
// if we've moved past the last real slide, jump back to the equivalent real index
if (currentIndex >= originalCount + visibleCount) {
// disable transition for the jump
track.style.transition = 'none';
currentIndex = visibleCount;
updatePosition();
// allow transitions again
void track.offsetWidth;
track.style.transition = 'transform 0.5s ease';
}
});
// Auto rotate
function startAutoRotate(interval = 3000) {
stopAutoRotate();
autoTimer = setInterval(nextSlide, interval);
}
function stopAutoRotate() {
if (autoTimer) {
clearInterval(autoTimer);
autoTimer = null;
}
}
// Recompute sizes on resize
window.addEventListener('resize', () => {
// A full re-initialization is safer on resize if visibleCount changes
stopAutoRotate();
init(); // Re-run the whole setup
});
// Initialize sizes and start
setSlideSizes();
startAutoRotate(2500);
// Optional: pause on hover
carousel.addEventListener('mouseenter', stopAutoRotate);
carousel.addEventListener('mouseleave', () => startAutoRotate(2500));
}