-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyScript.js
More file actions
29 lines (28 loc) · 855 Bytes
/
myScript.js
File metadata and controls
29 lines (28 loc) · 855 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
25
26
27
28
29
let nav = Array.from(document.querySelectorAll('.nav-link'))
.reduce(function (acc, navElem) {
if ((attr = navElem.getAttribute('href')).match(/^#.*/g)) {
acc.push(
[attr == '#'
? document.getElementById('first')
: document.getElementById(attr.substr(1)),
navElem
]
)
}
return acc;
}, []);
window.onscroll = () => {
for (const [elem, navElem] of nav) {
if (elem.getAttribute('id') === 'first'
&& this.scrollY <= elem.offsetHeight + elem.offsetTop) {
navElem.classList.add('nav-link-jshover'); continue;
}
if (this.scrollY > elem.offsetTop - 70 // navbar height
&& this.scrollY < elem.offsetTop + elem.offsetHeight
) {
navElem.classList.add('nav-link-jshover');
} else {
navElem.classList.remove('nav-link-jshover');
}
}
}