-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (88 loc) · 3.1 KB
/
script.js
File metadata and controls
107 lines (88 loc) · 3.1 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
// Auto-scroll reviews with infinite loop
const carousel = document.querySelector(".carousel");
const reviews = document.querySelectorAll(".review");
const prevBtn = document.querySelector(".prev");
const nextBtn = document.querySelector(".next");
let currentIndex = 2; // Start with the middle review
let autoScrollInterval;
function updateCarousel() {
// Remove all active classes
reviews.forEach(review => review.classList.remove("active"));
// Calculate the middle index (current) and adjacent indices
const middleIndex = currentIndex;
const prevIndex = (middleIndex - 1 + reviews.length) % reviews.length;
const nextIndex = (middleIndex + 1) % reviews.length;
// Set active class to the middle review
reviews[middleIndex].classList.add("active");
// Set adjacent reviews to visible but inactive
reviews[prevIndex].style.opacity = "0.3";
reviews[nextIndex].style.opacity = "0.3";
// Hide all other reviews
reviews.forEach((review, index) => {
if (index !== middleIndex && index !== prevIndex && index !== nextIndex) {
review.style.opacity = "0";
}
});
// Calculate transform value to center the active review
const itemWidth = reviews[0].offsetWidth + 30; // width + gap
const transformValue = -currentIndex * itemWidth + (carousel.offsetWidth / 2 - itemWidth / 2);
carousel.style.transform = `translateX(${transformValue}px)`;
}
function nextReview() {
currentIndex = (currentIndex + 1) % reviews.length;
updateCarousel();
}
function prevReview() {
currentIndex = (currentIndex - 1 + reviews.length) % reviews.length;
updateCarousel();
}
function startAutoScroll() {
autoScrollInterval = setInterval(nextReview, 3000);
}
function stopAutoScroll() {
clearInterval(autoScrollInterval);
}
// Initialize carousel
updateCarousel();
startAutoScroll();
// Button event listeners
nextBtn.addEventListener("click", () => {
stopAutoScroll();
nextReview();
startAutoScroll();
});
prevBtn.addEventListener("click", () => {
stopAutoScroll();
prevReview();
startAutoScroll();
});
// Pause auto-scroll on hover
carousel.addEventListener("mouseenter", stopAutoScroll);
carousel.addEventListener("mouseleave", startAutoScroll);
// Dark mode toggle
document.getElementById("dark-toggle").addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
});
// Scroll arrow buttons
const sections = document.querySelectorAll('.section');
let sectionIndex = 0;
document.getElementById('scroll-down').addEventListener('click', () => {
if (sectionIndex < sections.length - 1) {
sectionIndex++;
sections[sectionIndex].scrollIntoView({ behavior: 'smooth' });
}
});
document.getElementById('scroll-up').addEventListener('click', () => {
if (sectionIndex > 0) {
sectionIndex--;
sections[sectionIndex].scrollIntoView({ behavior: 'smooth' });
}
});
document.addEventListener('scroll', () => {
const scrollY = window.scrollY;
sections.forEach((section, i) => {
if (section.offsetTop <= scrollY + 100) {
sectionIndex = i;
}
});
});