-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
74 lines (64 loc) · 2.17 KB
/
Copy pathscript.js
File metadata and controls
74 lines (64 loc) · 2.17 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
const body = document.body;
const menuToggle = document.getElementById('menuToggle');
const nav = document.getElementById('siteNav');
const navLinks = nav ? [...nav.querySelectorAll('a')] : [];
const sections = [...document.querySelectorAll('main section[id]')];
const revealTargets = [...document.querySelectorAll('.reveal')];
if (menuToggle) {
menuToggle.addEventListener('click', () => {
const expanded = menuToggle.getAttribute('aria-expanded') === 'true';
menuToggle.setAttribute('aria-expanded', String(!expanded));
body.classList.toggle('menu-open', !expanded);
});
}
navLinks.forEach((link) => {
link.addEventListener('click', () => {
body.classList.remove('menu-open');
menuToggle?.setAttribute('aria-expanded', 'false');
});
});
function updateActiveNav() {
let current = sections[0]?.id || '';
sections.forEach((section) => {
const rect = section.getBoundingClientRect();
if (rect.top <= window.innerHeight * 0.28) {
current = section.id;
}
});
navLinks.forEach((link) => {
const href = link.getAttribute('href') || '';
link.classList.toggle('is-active', href === `#${current}`);
});
}
function updateHeaderState() {
body.classList.toggle('nav-scrolled', window.scrollY > 12);
}
const observer = 'IntersectionObserver' in window
? new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add('is-visible');
observer.unobserve(entry.target);
}
});
}, { threshold: 0.12, rootMargin: '0px 0px -8% 0px' })
: null;
revealTargets.forEach((target) => {
if (observer) {
observer.observe(target);
} else {
target.classList.add('is-visible');
}
});
updateActiveNav();
updateHeaderState();
window.addEventListener('scroll', updateActiveNav, { passive: true });
window.addEventListener('scroll', updateHeaderState, { passive: true });
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('./service-worker.js').catch(() => {});
});
}
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
body.classList.add('reduced-motion');
}