-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
228 lines (193 loc) · 7.16 KB
/
Copy pathscript.js
File metadata and controls
228 lines (193 loc) · 7.16 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Smooth scroll with momentum effect
document.addEventListener('DOMContentLoaded', function() {
const wrapper = document.getElementById('scrollWrapper');
const sections = document.querySelectorAll('.scroll-section');
const nav = document.getElementById('navbar');
const progressBar = document.getElementById('progressBar');
let currentSection = 0;
let isScrolling = false;
let scrollTimeout;
let touchStartY = 0;
let isUserScrolling = false;
// Create progress indicator dots
createProgressDots();
// Function to create navigation dots
function createProgressDots() {
const indicator = document.createElement('div');
indicator.className = 'progress-indicator';
indicator.id = 'progressIndicator';
sections.forEach((section, index) => {
const dot = document.createElement('div');
dot.className = `progress-dot ${index === 0 ? 'active' : ''}`;
dot.dataset.index = index;
dot.addEventListener('click', () => scrollToSection(index));
indicator.appendChild(dot);
});
document.body.appendChild(indicator);
}
// Update active dot
function updateActiveDot(index) {
document.querySelectorAll('.progress-dot').forEach((dot, i) => {
dot.classList.toggle('active', i === index);
});
}
// Scroll to specific section
function scrollToSection(index) {
if (index < 0 || index >= sections.length) return;
currentSection = index;
isScrolling = true;
sections[index].scrollIntoView({
behavior: 'smooth',
block: 'start'
});
updateActiveDot(index);
updateProgressBar();
// Reset scrolling flag after animation
setTimeout(() => {
isScrolling = false;
}, 800);
}
// Update progress bar
function updateProgressBar() {
const progress = ((currentSection + 1) / sections.length) * 100;
progressBar.style.width = progress + '%';
}
// Handle wheel scroll
wrapper.addEventListener('wheel', function(e) {
e.preventDefault();
if (isScrolling) return;
if (e.deltaY > 0) {
// Scroll down
if (currentSection < sections.length - 1) {
scrollToSection(currentSection + 1);
}
} else {
// Scroll up
if (currentSection > 0) {
scrollToSection(currentSection - 1);
}
}
}, { passive: false });
// Handle touch events for mobile
wrapper.addEventListener('touchstart', function(e) {
touchStartY = e.touches[0].clientY;
isUserScrolling = true;
});
wrapper.addEventListener('touchmove', function(e) {
if (!isUserScrolling) return;
const touchY = e.touches[0].clientY;
const deltaY = touchStartY - touchY;
if (Math.abs(deltaY) > 50 && !isScrolling) {
if (deltaY > 0) {
// Swipe up
if (currentSection < sections.length - 1) {
scrollToSection(currentSection + 1);
}
} else {
// Swipe down
if (currentSection > 0) {
scrollToSection(currentSection - 1);
}
}
isUserScrolling = false;
}
e.preventDefault();
}, { passive: false });
wrapper.addEventListener('touchend', function() {
isUserScrolling = false;
});
// Handle keyboard navigation
document.addEventListener('keydown', function(e) {
if (isScrolling) return;
switch(e.key) {
case 'ArrowDown':
case ' ':
case 'PageDown':
e.preventDefault();
if (currentSection < sections.length - 1) {
scrollToSection(currentSection + 1);
}
break;
case 'ArrowUp':
case 'PageUp':
e.preventDefault();
if (currentSection > 0) {
scrollToSection(currentSection - 1);
}
break;
case 'Home':
e.preventDefault();
scrollToSection(0);
break;
case 'End':
e.preventDefault();
scrollToSection(sections.length - 1);
break;
}
});
// Handle intersection observer for scroll events
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting && !isScrolling) {
const index = Array.from(sections).indexOf(entry.target);
currentSection = index;
updateActiveDot(index);
updateProgressBar();
}
});
}, {
threshold: 0.5
});
sections.forEach(section => observer.observe(section));
// Nav link click handlers
document.querySelectorAll('nav a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
if (targetId === '#') return;
const targetSection = document.querySelector(targetId);
if (!targetSection) return;
const index = Array.from(sections).indexOf(targetSection);
if (index !== -1) {
scrollToSection(index);
}
});
});
// Mobile menu link handlers
document.querySelectorAll('#mobile-menu a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
if (targetId === '#') return;
const targetSection = document.querySelector(targetId);
if (!targetSection) return;
const index = Array.from(sections).indexOf(targetSection);
if (index !== -1) {
scrollToSection(index);
toggleMobileMenu();
}
});
});
// Initialize
updateProgressBar();
// Smooth scroll on page load
setTimeout(() => {
wrapper.style.scrollBehavior = 'smooth';
}, 100);
// Enhanced nav scroll effect
window.addEventListener('scroll', () => {
const scrollTop = wrapper.scrollTop;
if (scrollTop > 20) {
nav.classList.add('shadow-lg');
} else {
nav.classList.remove('shadow-lg');
}
});
});
// Keep your existing toggleMobileMenu function
function toggleMobileMenu() {
const menu = document.getElementById('mobile-menu');
menu.classList.toggle('hidden');
menu.classList.toggle('animate__animated');
menu.classList.toggle('animate__fadeInDown');
}