-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
270 lines (238 loc) · 10.2 KB
/
script.js
File metadata and controls
270 lines (238 loc) · 10.2 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
document.addEventListener('DOMContentLoaded', () => {
// Reveal on scroll
const reveals = document.querySelectorAll('.reveal');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
observer.unobserve(entry.target);
}
});
}, { threshold: 0.1, rootMargin: '0px 0px -40px 0px' });
reveals.forEach(el => observer.observe(el));
// Nav scroll effect
const nav = document.getElementById('nav');
let ticking = false;
window.addEventListener('scroll', () => {
if (!ticking) {
requestAnimationFrame(() => {
nav.classList.toggle('nav-scrolled', window.scrollY > 50);
ticking = false;
});
ticking = true;
}
});
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(a => {
a.addEventListener('click', e => {
e.preventDefault();
const t = document.querySelector(a.getAttribute('href'));
if (t) t.scrollIntoView({ behavior: 'smooth', block: 'start' });
});
});
// FAQ accordion
document.querySelectorAll('.faq-trigger').forEach(btn => {
btn.addEventListener('click', () => {
const item = btn.parentElement;
const open = item.classList.contains('active');
document.querySelectorAll('.faq-item.active').forEach(i => {
i.classList.remove('active');
i.querySelector('.faq-trigger').setAttribute('aria-expanded', 'false');
});
if (!open) {
item.classList.add('active');
btn.setAttribute('aria-expanded', 'true');
}
});
});
// Template card scroll on hover
document.querySelectorAll('.template-card').forEach(card => {
const preview = card.querySelector('.template-preview');
const scroll = card.querySelector('.template-scroll');
if (!preview || !scroll) return;
card.addEventListener('mouseenter', () => {
const maxScroll = -(scroll.scrollHeight - preview.clientHeight);
scroll.style.transform = `translateY(${maxScroll}px)`;
});
card.addEventListener('mouseleave', () => {
scroll.style.transform = 'translateY(0)';
});
card.addEventListener('touchstart', () => {
const maxScroll = -(scroll.scrollHeight - preview.clientHeight);
scroll.style.transform = `translateY(${maxScroll}px)`;
}, { passive: true });
card.addEventListener('touchend', () => {
scroll.style.transform = 'translateY(0)';
});
});
// Template modal / lightbox
const modal = document.getElementById('templateModal');
const modalPreview = modal.querySelector('.template-modal-preview');
const modalClose = modal.querySelector('.template-modal-close');
const modalPrev = modal.querySelector('.template-modal-prev');
const modalNext = modal.querySelector('.template-modal-next');
const modalBackdrop = modal.querySelector('.template-modal-backdrop');
const cards = [...document.querySelectorAll('.template-card')];
let currentIndex = 0;
let rafId = null;
let mouseY = null;
let hintTimeout = null;
let hintDismissed = false;
let cleanupTimeout = null;
const modalHint = modalPreview.querySelector('.template-modal-hint');
function onModalMouseMove(e) {
const rect = modalPreview.getBoundingClientRect();
mouseY = (e.clientY - rect.top) / rect.height;
if (!hintDismissed && modalHint) {
hintDismissed = true;
modalHint.classList.remove('visible');
}
}
function onModalMouseLeave() {
mouseY = null;
}
function scrollTick() {
if (mouseY !== null) {
const maxScroll = modalPreview.scrollHeight - modalPreview.clientHeight;
const normalized = (mouseY - 0.5) * 2; // -1 to 1
const deadzone = 0.3;
let speed = 0;
if (Math.abs(normalized) > deadzone) {
speed = (Math.abs(normalized) - deadzone) / (1 - deadzone);
speed = speed * speed * 4;
speed *= Math.sign(normalized);
}
modalPreview.scrollTop = Math.max(0, Math.min(maxScroll, modalPreview.scrollTop + speed));
}
rafId = requestAnimationFrame(scrollTick);
}
function openModal(card) {
currentIndex = cards.indexOf(card);
const scrollEl = card.querySelector('.template-scroll');
if (!scrollEl) return;
const clone = scrollEl.cloneNode(true);
clone.style.transform = 'none';
clone.style.width = (modalPreview.clientWidth / 2) + 'px';
clearTimeout(cleanupTimeout);
Array.from(modalPreview.children).forEach(child => {
if (child !== modalHint) modalPreview.removeChild(child);
});
modalPreview.insertBefore(clone, modalHint);
modalPreview.scrollTop = 0;
modal.classList.add('active');
document.body.style.overflow = 'hidden';
hintDismissed = false;
if (modalHint) {
modalHint.classList.remove('visible');
hintTimeout = setTimeout(() => modalHint.classList.add('visible'), 400);
}
mouseY = null;
modalPreview.addEventListener('mousemove', onModalMouseMove);
modalPreview.addEventListener('mouseleave', onModalMouseLeave);
rafId = requestAnimationFrame(scrollTick);
}
function closeModal() {
cancelAnimationFrame(rafId);
clearTimeout(hintTimeout);
mouseY = null;
if (modalHint) modalHint.classList.remove('visible');
modalPreview.removeEventListener('mousemove', onModalMouseMove);
modalPreview.removeEventListener('mouseleave', onModalMouseLeave);
modal.classList.remove('active');
document.body.style.overflow = '';
cleanupTimeout = setTimeout(() => {
Array.from(modalPreview.children).forEach(child => {
if (child !== modalHint) modalPreview.removeChild(child);
});
}, 300);
}
function navigateModal(direction) {
if (!modal.classList.contains('active')) return;
currentIndex = (currentIndex + direction + cards.length) % cards.length;
const card = cards[currentIndex];
const scrollEl = card.querySelector('.template-scroll');
if (!scrollEl) return;
const clone = scrollEl.cloneNode(true);
clone.style.transform = 'none';
clone.style.width = (modalPreview.clientWidth / 2) + 'px';
clearTimeout(cleanupTimeout);
Array.from(modalPreview.children).forEach(child => {
if (child !== modalHint) modalPreview.removeChild(child);
});
modalPreview.insertBefore(clone, modalHint);
modalPreview.scrollTop = 0;
hintDismissed = false;
clearTimeout(hintTimeout);
if (modalHint) {
modalHint.classList.remove('visible');
hintTimeout = setTimeout(() => modalHint.classList.add('visible'), 400);
}
}
document.querySelectorAll('.template-card .template-browser').forEach(browser => {
browser.addEventListener('click', () => {
openModal(browser.closest('.template-card'));
});
});
modalClose.addEventListener('click', closeModal);
modalPrev.addEventListener('click', () => navigateModal(-1));
modalNext.addEventListener('click', () => navigateModal(1));
modalBackdrop.addEventListener('click', closeModal);
document.addEventListener('keydown', (e) => {
if (!modal.classList.contains('active')) return;
if (e.key === 'Escape') closeModal();
if (e.key === 'ArrowLeft') navigateModal(-1);
if (e.key === 'ArrowRight') navigateModal(1);
});
// Contact form
const form = document.querySelector('.contact-form');
if (form) {
form.querySelectorAll('[required]').forEach(input => {
input.addEventListener('input', () => input.classList.remove('invalid'));
});
form.addEventListener('submit', (e) => {
e.preventDefault();
const inputs = form.querySelectorAll('[required]');
let valid = true;
inputs.forEach(input => {
const val = input.value.trim();
if (!val || (input.type === 'email' && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val))) {
input.classList.add('invalid');
valid = false;
} else {
input.classList.remove('invalid');
}
});
if (!valid) return;
const formData = new FormData(form);
fetch('/', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams(formData).toString()
}).then(() => {
const success = document.createElement('div');
success.className = 'form-success';
const h3 = document.createElement('h3');
h3.textContent = 'Sent!';
const p = document.createElement('p');
p.textContent = "I'll get back to you within 24 hours. Probably sooner.";
success.appendChild(h3);
success.appendChild(p);
form.replaceChildren(success);
}).catch(() => {
const error = document.createElement('div');
error.className = 'form-success';
const h3 = document.createElement('h3');
h3.textContent = 'Something broke (ironic, I know)';
const p = document.createElement('p');
p.textContent = 'Email me directly at ';
const a = document.createElement('a');
a.href = 'mailto:fixedfastdev@protonmail.com';
a.textContent = 'fixedfastdev@protonmail.com';
p.appendChild(a);
error.appendChild(h3);
error.appendChild(p);
form.replaceChildren(error);
});
});
}
});