-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (54 loc) · 1.59 KB
/
Copy pathscript.js
File metadata and controls
64 lines (54 loc) · 1.59 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
const yearNode = document.querySelector("#year");
const navToggle = document.querySelector(".nav-toggle");
const nav = document.querySelector(".site-nav");
const navLinks = [...document.querySelectorAll(".site-nav a")];
const sections = navLinks
.map((link) => document.querySelector(link.getAttribute("href")))
.filter(Boolean);
if (yearNode) {
yearNode.textContent = new Date().getFullYear();
}
const setNavState = (isOpen) => {
if (!navToggle || !nav) {
return;
}
navToggle.setAttribute("aria-expanded", String(isOpen));
nav.setAttribute("data-open", String(isOpen));
};
if (navToggle && nav) {
navToggle.addEventListener("click", () => {
const isOpen = navToggle.getAttribute("aria-expanded") === "true";
setNavState(!isOpen);
});
navLinks.forEach((link) => {
link.addEventListener("click", () => {
if (window.innerWidth <= 700) {
setNavState(false);
}
});
});
window.addEventListener("resize", () => {
if (window.innerWidth > 700) {
setNavState(false);
}
});
}
const observer = new IntersectionObserver(
(entries) => {
const visible = entries
.filter((entry) => entry.isIntersecting)
.sort((a, b) => b.intersectionRatio - a.intersectionRatio)[0];
if (!visible?.target?.id) {
return;
}
navLinks.forEach((link) => {
const isActive = link.getAttribute("href") === `#${visible.target.id}`;
link.classList.toggle("is-active", isActive);
});
},
{
rootMargin: "-35% 0px -45% 0px",
threshold: [0.2, 0.5, 0.8],
}
);
sections.forEach((section) => observer.observe(section));