-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactive.js
37 lines (30 loc) · 982 Bytes
/
active.js
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
const navLinks = document.querySelectorAll(".navig");
function updateActiveLink() {
const height = window.innerHeight;
const scrollPosition = window.scrollY;
const sectionOffsets = [
document.querySelector("#about").offsetTop,
document.querySelector("#experience").offsetTop - 100,
document.querySelector("#projects").offsetTop - 300,
document.querySelector("#opensource").offsetTop - 900,
document.querySelector("#awards").offsetTop,
];
let activeIndex = 0;
for (let i = sectionOffsets.length - 1; i >= 0; i--) {
if (
window.innerHeight + Math.round(window.scrollY) >=
document.body.offsetHeight
) {
activeIndex = 4;
break;
}
if (scrollPosition >= sectionOffsets[i]) {
activeIndex = i;
break;
}
}
navLinks.forEach((link) => link.classList.remove("active"));
navLinks[activeIndex].classList.add("active");
}
window.addEventListener("scroll", updateActiveLink);
updateActiveLink();