-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
281 lines (250 loc) · 9.05 KB
/
Copy pathapp.js
File metadata and controls
281 lines (250 loc) · 9.05 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
(() => {
'use strict';
// Utility: throttle
const throttle = (fn, wait = 100) => {
let last = 0;
return (...args) => {
const now = Date.now();
if (now - last >= wait) {
last = now;
fn(...args);
}
};
};
// Smooth scroll for internal anchor links
function initSmoothScrolling() {
// Use native smooth behavior where available
try {
document.documentElement.style.scrollBehavior = 'smooth';
} catch (e) {}
document.addEventListener('click', (e) => {
const anchor = e.target.closest('a[href^="#"]');
if (!anchor) return;
const href = anchor.getAttribute('href');
if (!href || href === '#') return;
const target = document.querySelector(href);
if (target) {
e.preventDefault();
// Prefer native smooth scrolling
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
// update URL hash without jumping
history.replaceState(null, '', href);
}
});
}
// Reveal on scroll
function initRevealOnScroll() {
const selectors = [
'.feature-card',
'.stat-card',
'.benefit-item',
'.comparison-column',
'.inspiration-image-container',
'.big-picture-image',
'.books-image',
'.hero-section',
'.section-title',
'.footer'
];
const elems = Array.from(document.querySelectorAll(selectors.join(',')));
elems.forEach((el) => {
// set initial hidden state
el.style.opacity = '0';
el.style.transform = 'translateY(18px)';
el.style.transition = 'opacity 560ms ease-out, transform 560ms ease-out';
el.setAttribute('data-revealed', 'false');
});
const io = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
const el = entry.target;
if (entry.isIntersecting && el.getAttribute('data-revealed') === 'false') {
el.style.opacity = '1';
el.style.transform = 'translateY(0)';
el.setAttribute('data-revealed', 'true');
}
});
}, { root: null, rootMargin: '0px 0px -8% 0px', threshold: 0.08 });
elems.forEach((el) => io.observe(el));
}
// Lazy load images and set loading attr
function initLazyImages() {
const imgs = Array.from(document.querySelectorAll('img'));
imgs.forEach((img) => {
if (!img.hasAttribute('loading')) {
img.setAttribute('loading', 'lazy');
}
});
// Optional: if images have data-src for progressive loading
const progressive = document.querySelectorAll('img[data-src]');
if (progressive.length) {
const io = new IntersectionObserver((entries, obs) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const im = entry.target;
im.src = im.dataset.src;
im.removeAttribute('data-src');
obs.unobserve(im);
}
});
}, { rootMargin: '200px' });
progressive.forEach((i) => io.observe(i));
}
}
// Mobile hamburger menu: create and wire menu behavior
function initMobileMenu() {
const header = document.querySelector('.header');
const headerContainer = document.querySelector('.header-container');
const nav = document.querySelector('.navigation');
if (!header || !headerContainer || !nav) return;
// create hamburger button if not present
if (!document.querySelector('.hamburger-toggle')) {
const btn = document.createElement('button');
btn.className = 'hamburger-toggle';
btn.type = 'button';
btn.setAttribute('aria-expanded', 'false');
btn.setAttribute('aria-label', 'Open menu');
btn.innerHTML = '<span class="hamburger-box"><span class="hamburger-inner"></span></span>';
headerContainer.insertBefore(btn, headerContainer.firstChild);
// basic styles appended via JS so they apply without editing CSS files
const style = document.createElement('style');
style.textContent = `
.hamburger-toggle{display:none;background:transparent;border:none;padding:8px;cursor:pointer}
.hamburger-box{display:inline-block;width:28px;height:18px;position:relative}
.hamburger-inner, .hamburger-inner::before, .hamburger-inner::after{display:block;width:28px;height:3px;background:#111;border-radius:2px;position:absolute;left:0}
.hamburger-inner{top:50%;transform:translateY(-50%)}
.hamburger-inner::before{content:'';top:-8px}
.hamburger-inner::after{content:'';top:8px}
.mobile-nav-overlay{position:fixed;inset:0;background:rgba(0,0,0,0.6);display:flex;justify-content:flex-end;z-index:9999}
.mobile-nav{width:320px;max-width:85%;background:#fff;height:100%;padding:40px 24px;overflow:auto}
.mobile-nav .nav-list{flex-direction:column;gap:20px}
.mobile-nav .nav-link{font-size:18px}
`;
document.head.appendChild(style);
// overlay and panel
let overlay = null;
function openMobileNav() {
if (overlay) return;
overlay = document.createElement('div');
overlay.className = 'mobile-nav-overlay';
const panel = document.createElement('nav');
panel.className = 'mobile-nav';
// clone nav-list for mobile
const cloned = nav.querySelector('.nav-list')?.cloneNode(true);
if (cloned) {
// make links close menu on click
cloned.querySelectorAll('a').forEach((a) => a.addEventListener('click', () => closeMobileNav()));
panel.appendChild(cloned);
}
// optional extra actions (browse button)
const browse = document.querySelector('.Browse');
if (browse) {
const b = browse.cloneNode(true);
b.classList.add('mobile-browse');
b.style.marginTop = '24px';
panel.appendChild(b);
}
overlay.appendChild(panel);
document.body.appendChild(overlay);
document.body.style.overflow = 'hidden';
btn.setAttribute('aria-expanded', 'true');
btn.setAttribute('aria-label', 'Close menu');
overlay.addEventListener('click', (ev) => {
if (ev.target === overlay) closeMobileNav();
});
// escape to close
document.addEventListener('keydown', onEscClose);
}
function closeMobileNav() {
if (!overlay) return;
overlay.remove();
overlay = null;
document.body.style.overflow = '';
btn.setAttribute('aria-expanded', 'false');
btn.setAttribute('aria-label', 'Open menu');
document.removeEventListener('keydown', onEscClose);
}
function onEscClose(e) {
if (e.key === 'Escape') closeMobileNav();
}
btn.addEventListener('click', () => {
const expanded = btn.getAttribute('aria-expanded') === 'true';
if (expanded) closeMobileNav(); else openMobileNav();
});
// show/hide hamburger based on width
const updateHamburgerVisibility = () => {
const w = window.innerWidth;
if (w <= 768) {
btn.style.display = 'inline-block';
// hide original nav to avoid duplicate interactive items
nav.style.display = 'none';
} else {
btn.style.display = 'none';
nav.style.display = '';
closeMobileNav();
}
};
updateHamburgerVisibility();
window.addEventListener('resize', throttle(updateHamburgerVisibility, 200));
}
}
// Highlight active nav link based on scroll position
function initNavHighlight() {
const links = Array.from(document.querySelectorAll('.nav-link[href^="#"]'));
if (!links.length) return;
const sections = links.map((a) => document.querySelector(a.getAttribute('href'))).filter(Boolean);
const io = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
const id = entry.target.id;
const link = document.querySelector(`.nav-link[href="#${id}"]`);
if (link) link.classList.toggle('active', entry.isIntersecting);
});
}, { threshold: 0.5 });
sections.forEach((s) => io.observe(s));
}
// Basic enhancements: ensure hero pagination dots are keyboard accessible
function enhancePaginationDots() {
const dots = Array.from(document.querySelectorAll('.pagination-dot'));
dots.forEach((d) => {
d.setAttribute('tabindex', '0');
d.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') d.click();
});
});
}
// On DOM ready initialize features
function init() {
initSmoothScrolling();
initRevealOnScroll();
initLazyImages();
initMobileMenu();
initNavHighlight();
enhancePaginationDots();
// small accessibility: ensure main landmark exists
if (!document.querySelector('main')) {
const m = document.createElement('main');
m.setAttribute('role', 'main');
// Not inserting content; prefer that existing HTML provides main element
}
// slight UX tweak: shrink header when scrolling
const header = document.querySelector('.header');
if (header) {
let lastY = window.scrollY;
const onScroll = throttle(() => {
const y = window.scrollY;
if (y > 40) {
header.style.transform = 'translateY(-4px) scale(0.995)';
header.style.transition = 'transform 220ms ease, box-shadow 220ms ease';
} else {
header.style.transform = '';
}
lastY = y;
}, 120);
window.addEventListener('scroll', onScroll, { passive: true });
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();