-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (58 loc) · 2.12 KB
/
script.js
File metadata and controls
72 lines (58 loc) · 2.12 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
const slides = document.querySelectorAll('.slide');
const indicatorsContainer = document.querySelector('.indicators');
let currentIndex = 0;
const slideInterval = 1500; // Time in milliseconds for each slide (1 second=1000)
// Show the current slide
function showSlide(index) {
const slideWidth = slides[0].clientWidth;
document.querySelector('.slides').style.transform = `translateX(-${index * slideWidth}px)`;
slides.forEach((slide, i) => {
slide.style.opacity = i === index ? 1 : 0;
});
document.querySelectorAll('.indicator').forEach((indicator, i) => {
indicator.classList.toggle('active', i === index);
});
currentIndex = index;
}
// Show the next slide
function nextSlide() {
const nextIndex = (currentIndex + 1) % slides.length;
showSlide(nextIndex);
}
// Show the previous slide
function prevSlide() {
const prevIndex = (currentIndex - 1 + slides.length) % slides.length;
showSlide(prevIndex);
}
// Create indicators
slides.forEach((_, i) => {
const indicator = document.createElement('div');
indicator.classList.add('indicator');
indicator.addEventListener('click', () => showSlide(i));
indicatorsContainer.appendChild(indicator);
});
// Start autoplay
let autoPlay = setInterval(nextSlide, slideInterval);
// Handle slider mouseover and mouseout
document.querySelector('.slider').addEventListener('mouseover', () => clearInterval(autoPlay));
document.querySelector('.slider').addEventListener('mouseout', () => autoPlay = setInterval(nextSlide, slideInterval));
// Swipe support for touch devices
let touchStartX = 0;
let touchEndX = 0;
function handleSwipe() {
if (touchEndX < touchStartX) {
nextSlide();
}
if (touchEndX > touchStartX) {
prevSlide();
}
}
document.querySelector('.slider').addEventListener('touchstart', e => {
touchStartX = e.changedTouches[0].screenX;
});
document.querySelector('.slider').addEventListener('touchend', e => {
touchEndX = e.changedTouches[0].screenX;
handleSwipe();
});
// Show the initial slide
showSlide(currentIndex);