-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (28 loc) · 958 Bytes
/
script.js
File metadata and controls
33 lines (28 loc) · 958 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
32
33
const navToggle = document.querySelector('.nav-toggle');
const navLinks = document.querySelector('.nav-links');
const navItems = document.querySelectorAll('.nav-links a');
if (navToggle && navLinks) {
navToggle.addEventListener('click', () => {
const isOpen = navLinks.classList.toggle('open');
navToggle.setAttribute('aria-expanded', String(isOpen));
});
}
const sections = document.querySelectorAll('section[id]');
const setActiveLink = () => {
let current = '';
sections.forEach((section) => {
const sectionTop = section.offsetTop - 120;
if (window.scrollY >= sectionTop) {
current = section.getAttribute('id');
}
});
navItems.forEach((link) => {
link.classList.toggle('active', link.getAttribute('href') === `#${current}`);
});
};
window.addEventListener('scroll', setActiveLink);
setActiveLink();
const year = document.getElementById('year');
if (year) {
year.textContent = new Date().getFullYear();
}