forked from rohanpandula/ScanStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
24 lines (21 loc) · 885 Bytes
/
Copy pathscript.js
File metadata and controls
24 lines (21 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const sectionLinks = [...document.querySelectorAll('.site-nav a[href^="#"]')];
const sections = sectionLinks
.map((link) => document.querySelector(link.getAttribute("href")))
.filter(Boolean);
if ("IntersectionObserver" in window && sections.length) {
const observer = new IntersectionObserver(
(entries) => {
const active = entries
.filter((entry) => entry.isIntersecting)
.sort((a, b) => b.intersectionRatio - a.intersectionRatio)[0];
if (!active) return;
sectionLinks.forEach((link) => {
const matches = link.getAttribute("href") === `#${active.target.id}`;
if (matches) link.setAttribute("aria-current", "location");
else link.removeAttribute("aria-current");
});
},
{ rootMargin: "-28% 0px -58%", threshold: [0.12, 0.4, 0.7] },
);
sections.forEach((section) => observer.observe(section));
}