-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
32 lines (26 loc) · 915 Bytes
/
Copy pathscript.js
File metadata and controls
32 lines (26 loc) · 915 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
// Mobile menu toggle
const mobileMenu = document.getElementById("mobile-menu");
const navLinks = document.querySelector(".nav-links");
mobileMenu.addEventListener("click", () => {
navLinks.classList.toggle("active");
});
// Scroll spy
const sections = document.querySelectorAll("section, footer");
const links = document.querySelectorAll(".nav-link");
window.addEventListener("scroll", () => {
let current = "";
const scrollY = window.scrollY;
sections.forEach(section => {
const sectionTop = section.offsetTop - 70;
const sectionHeight = section.clientHeight;
if (scrollY >= sectionTop && scrollY < sectionTop + sectionHeight) {
current = section.getAttribute("id");
}
});
links.forEach(link => {
link.classList.remove("active");
if (link.getAttribute("href") === `#${current}`) {
link.classList.add("active");
}
});
});