-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (28 loc) · 979 Bytes
/
Copy pathscript.js
File metadata and controls
31 lines (28 loc) · 979 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
30
31
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('.nav-link');
function updateActiveLink() {
let currentId = '';
sections.forEach(section => {
if (window.scrollY >= section.offsetTop - 160) {
currentId = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === '#' + currentId) {
link.classList.add('active');
}
});
}
window.addEventListener('scroll', updateActiveLink, { passive: true });
updateActiveLink();
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
const href = this.getAttribute('href');
if (href.startsWith('#')) {
e.preventDefault();
const target = document.querySelector(href);
if (target) target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
});
});