Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (46 loc) · 1.69 KB
/
Copy pathscript.js
File metadata and controls
57 lines (46 loc) · 1.69 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
const container = document.getElementById('slides');
const slides = document.querySelectorAll('.slide-item');
const prevBtn = document.querySelector('.prev-btn');
const nextBtn = document.querySelector('.next-btn');
const pagination = document.getElementById('pagination');
let currentIndex = 0;
const totalSlides = slides.length;
// Init Pagination
slides.forEach((_, i) => {
const dot = document.createElement('div');
dot.classList.add('dot');
if (i === 0) dot.classList.add('active');
dot.addEventListener('click', () => goToSlide(i));
pagination.appendChild(dot);
});
const dots = document.querySelectorAll('.dot');
function updateSlider() {
container.style.transform = `translateX(-${currentIndex * 100}%)`;
dots.forEach((dot, i) => {
dot.classList.toggle('active', i === currentIndex);
});
}
function nextSlide() {
currentIndex = (currentIndex + 1) % totalSlides;
updateSlider();
}
function prevSlide() {
currentIndex = (currentIndex - 1 + totalSlides) % totalSlides;
updateSlider();
}
function goToSlide(index) {
currentIndex = index;
updateSlider();
}
nextBtn.addEventListener('click', nextSlide);
prevBtn.addEventListener('click', prevSlide);
// Optional: Keyboard Navigation
document.addEventListener('keydown', (e) => {
if (e.key === "ArrowLeft") prevSlide();
if (e.key === "ArrowRight") nextSlide();
});
// Auto-slide every 6 seconds
let autoPlay = setInterval(nextSlide, 6000);
// Pause auto-play on hover
document.querySelector('.slider-wrapper').addEventListener('mouseenter', () => clearInterval(autoPlay));
document.querySelector('.slider-wrapper').addEventListener('mouseleave', () => autoPlay = setInterval(nextSlide, 6000));