-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
140 lines (117 loc) · 3.06 KB
/
Copy pathscript.js
File metadata and controls
140 lines (117 loc) · 3.06 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
const body = document.body;
const topbar = document.querySelector(".topbar");
const menuToggle = document.querySelector(".menu-toggle");
const navLinks = [...document.querySelectorAll(".nav-links a")];
const sections = [...document.querySelectorAll("section[id]")];
const revealTargets = [...document.querySelectorAll("[data-reveal]")];
const toTop = document.querySelector(".to-top");
const closeMenu = () => {
body.classList.remove("nav-open");
menuToggle?.setAttribute("aria-expanded", "false");
};
const openMenu = () => {
body.classList.add("nav-open");
menuToggle?.setAttribute("aria-expanded", "true");
};
const setActiveLink = (id) => {
navLinks.forEach((link) => {
const isActive = link.getAttribute("href") === `#${id}`;
link.classList.toggle("active", isActive);
});
};
menuToggle?.addEventListener("click", () => {
const expanded = menuToggle.getAttribute("aria-expanded") === "true";
if (expanded) {
closeMenu();
} else {
openMenu();
}
});
navLinks.forEach((link) => {
link.addEventListener("click", (event) => {
event.preventDefault();
const href = link.getAttribute("href");
const target = href ? document.querySelector(href) : null;
closeMenu();
if (!target) {
return;
}
target.scrollIntoView({
behavior: "smooth",
block: "start",
});
});
});
document.addEventListener("keydown", (event) => {
if (event.key === "Escape") {
closeMenu();
}
});
document.addEventListener("click", (event) => {
if (!body.classList.contains("nav-open")) {
return;
}
if (topbar?.contains(event.target)) {
return;
}
closeMenu();
});
window.addEventListener("resize", () => {
if (window.innerWidth > 900) {
closeMenu();
}
});
const sectionObserver = new IntersectionObserver(
(entries) => {
const visibleEntries = entries
.filter((entry) => entry.isIntersecting)
.sort((a, b) => b.intersectionRatio - a.intersectionRatio);
if (!visibleEntries.length) {
return;
}
const currentId = visibleEntries[0].target.getAttribute("id");
if (currentId) {
setActiveLink(currentId);
}
},
{
rootMargin: "-28% 0px -45% 0px",
threshold: [0.2, 0.35, 0.55, 0.75],
},
);
sections.forEach((section) => sectionObserver.observe(section));
const revealObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) {
return;
}
entry.target.classList.add("is-visible");
observer.unobserve(entry.target);
});
},
{
rootMargin: "0px 0px -10% 0px",
threshold: 0.12,
},
);
revealTargets.forEach((target, index) => {
target.style.transitionDelay = `${Math.min(index * 40, 180)}ms`;
revealObserver.observe(target);
});
window.addEventListener("scroll", () => {
if (window.scrollY > 420) {
toTop?.classList.add("visible");
} else {
toTop?.classList.remove("visible");
}
});
toTop?.addEventListener("click", () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
});
if (sections[0]) {
setActiveLink(sections[0].id);
}