-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnap-scroll.js
More file actions
324 lines (261 loc) · 8.75 KB
/
snap-scroll.js
File metadata and controls
324 lines (261 loc) · 8.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Snap-scroll section controller
(function() {
'use strict';
// Check for reduced motion preference
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Prevent browser scroll restoration
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
class SnapScroll {
constructor() {
this.sections = Array.from(document.querySelectorAll('.section'));
this.navItems = document.querySelectorAll('.nav-item');
this.footer = document.querySelector('.site-footer');
this.currentIndex = 0;
this.isTransitioning = false;
this.transitionDuration = prefersReducedMotion ? 0 : 600;
this.callbacks = {
onTransitionStart: [],
onTransitionEnd: []
};
// Touch support
this.touchStartY = 0;
this.touchThreshold = 50;
// Debounce wheel events
this.wheelTimeout = null;
this.wheelCooldown = 800;
if (this.sections.length === 0) return;
this.init();
}
init() {
// Set initial state
this.sections.forEach((section, index) => {
section.setAttribute('data-index', index);
if (index === 0) {
section.classList.add('active');
}
});
// Update footer visibility
this.updateFooterVisibility();
// Bind event listeners
this.bindEvents();
// Update nav highlight
this.updateNavHighlight();
// Trigger initial stagger animation
this.triggerStaggerAnimation(this.sections[0]);
}
bindEvents() {
// Wheel event
window.addEventListener('wheel', (e) => this.handleWheel(e), { passive: false });
// Keyboard navigation
window.addEventListener('keydown', (e) => this.handleKeydown(e));
// Touch events for mobile
window.addEventListener('touchstart', (e) => this.handleTouchStart(e), { passive: true });
window.addEventListener('touchend', (e) => this.handleTouchEnd(e), { passive: true });
// Nav click events
this.navItems.forEach(item => {
item.addEventListener('click', (e) => this.handleNavClick(e));
});
}
handleWheel(e) {
// Skip if modal is open
if (this.isModalOpen()) return;
// Skip if transitioning or in cooldown
if (this.isTransitioning || this.wheelTimeout) return;
e.preventDefault();
const direction = e.deltaY > 0 ? 1 : -1;
const targetIndex = this.currentIndex + direction;
if (targetIndex >= 0 && targetIndex < this.sections.length) {
this.goToSection(targetIndex);
// Cooldown to prevent rapid firing
this.wheelTimeout = setTimeout(() => {
this.wheelTimeout = null;
}, this.wheelCooldown);
}
}
handleKeydown(e) {
// Skip if typing in input or modal is open
if (this.isTyping(e.target) || this.isModalOpen()) return;
switch (e.key) {
case 'ArrowDown':
case 'ArrowRight':
e.preventDefault();
this.next();
break;
case 'ArrowUp':
case 'ArrowLeft':
e.preventDefault();
this.prev();
break;
case 'Home':
e.preventDefault();
this.goToSection(0);
break;
case 'End':
e.preventDefault();
this.goToSection(this.sections.length - 1);
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
const index = parseInt(e.key) - 1;
if (index < this.sections.length) {
e.preventDefault();
this.goToSection(index);
}
break;
}
}
handleTouchStart(e) {
this.touchStartY = e.touches[0].clientY;
}
handleTouchEnd(e) {
if (this.isModalOpen()) return;
const touchEndY = e.changedTouches[0].clientY;
const deltaY = this.touchStartY - touchEndY;
if (Math.abs(deltaY) > this.touchThreshold) {
if (deltaY > 0) {
this.next();
} else {
this.prev();
}
}
}
handleNavClick(e) {
e.preventDefault();
const sectionId = e.currentTarget.getAttribute('data-section');
const targetSection = document.getElementById(sectionId);
if (targetSection) {
const targetIndex = this.sections.indexOf(targetSection);
if (targetIndex !== -1) {
this.goToSection(targetIndex);
}
}
}
goToSection(index) {
// Validate index
if (index < 0 || index >= this.sections.length || index === this.currentIndex) return;
if (this.isTransitioning) return;
this.isTransitioning = true;
const currentSection = this.sections[this.currentIndex];
const nextSection = this.sections[index];
const direction = index > this.currentIndex ? 'down' : 'up';
// Fire transition start callbacks
this.fireCallbacks('onTransitionStart', { from: this.currentIndex, to: index });
// Update nav highlight immediately
this.currentIndex = index;
this.updateNavHighlight();
// Reset stagger animations on current section
this.resetStaggerAnimation(currentSection);
// Animate out current section
currentSection.classList.add('fade-out');
currentSection.classList.add(direction === 'down' ? 'slide-up' : 'slide-down');
// Prepare next section
nextSection.classList.add('fade-in');
nextSection.classList.add(direction === 'down' ? 'from-below' : 'from-above');
// After transition
setTimeout(() => {
// Clean up current section
currentSection.classList.remove('active', 'fade-out', 'slide-up', 'slide-down');
// Activate next section
nextSection.classList.remove('fade-in', 'from-below', 'from-above');
nextSection.classList.add('active');
// Trigger stagger animation
this.triggerStaggerAnimation(nextSection);
// Update footer visibility
this.updateFooterVisibility();
// Update ARIA
this.updateAria();
this.isTransitioning = false;
// Fire transition end callbacks
this.fireCallbacks('onTransitionEnd', { index: this.currentIndex });
}, this.transitionDuration);
}
next() {
if (this.currentIndex < this.sections.length - 1) {
this.goToSection(this.currentIndex + 1);
}
}
prev() {
if (this.currentIndex > 0) {
this.goToSection(this.currentIndex - 1);
}
}
updateNavHighlight() {
const currentSectionId = this.sections[this.currentIndex].id;
this.navItems.forEach(item => {
const sectionId = item.getAttribute('data-section');
if (sectionId === currentSectionId) {
item.classList.add('active');
} else {
item.classList.remove('active');
}
});
}
updateFooterVisibility() {
if (!this.footer) return;
// Show footer only on contact section (last section)
const isLastSection = this.currentIndex === this.sections.length - 1;
this.footer.classList.toggle('visible', isLastSection);
}
updateAria() {
this.sections.forEach((section, index) => {
section.setAttribute('aria-hidden', index !== this.currentIndex);
});
}
triggerStaggerAnimation(section) {
const elements = section.querySelectorAll('.stagger-animate');
elements.forEach((el, index) => {
el.style.setProperty('--stagger-delay', index);
// Small delay to ensure CSS transition triggers
requestAnimationFrame(() => {
el.classList.add('animate-in');
});
});
}
resetStaggerAnimation(section) {
const elements = section.querySelectorAll('.stagger-animate');
elements.forEach(el => {
el.classList.remove('animate-in');
});
}
isTyping(target) {
return target.tagName === 'INPUT' ||
target.tagName === 'TEXTAREA' ||
target.isContentEditable;
}
isModalOpen() {
const modal = document.getElementById('flipbookModal');
return modal && modal.classList.contains('active');
}
// Callback system
onTransitionStart(callback) {
this.callbacks.onTransitionStart.push(callback);
}
onTransitionEnd(callback) {
this.callbacks.onTransitionEnd.push(callback);
}
fireCallbacks(event, data) {
this.callbacks[event].forEach(callback => callback(data));
}
// Get current section index
getCurrentIndex() {
return this.currentIndex;
}
}
// Initialize on DOM ready
function init() {
window.snapScroll = new SnapScroll();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
// Expose for external use
window.SnapScroll = SnapScroll;
})();