-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
30 lines (23 loc) · 1.1 KB
/
Copy pathscript.js
File metadata and controls
30 lines (23 loc) · 1.1 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
// Watches which section is on screen and highlights the right nav link
var sections = document.querySelectorAll('section[id]');
var navLinks = document.querySelectorAll('.nav-links a');
window.addEventListener('scroll', function () {
var scrollPosition = window.scrollY + 120; // 120px offset for the sticky navbar
sections.forEach(function (section) {
var sectionTop = section.offsetTop;
var sectionHeight = section.offsetHeight;
var sectionId = section.getAttribute('id');
// map section ids to nav hrefs
// "home" and "about" both live in the hero/about area → highlight "Home"
var navHref = '#' + sectionId;
if (sectionId === 'about') navHref = '#home'; // about is part of home scroll area
if (scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight) {
navLinks.forEach(function (link) {
link.classList.remove('active');
if (link.getAttribute('href') === navHref) {
link.classList.add('active');
}
});
}
});
});