const header = document.querySelector(".site-header"); const toggle = document.querySelector(".nav-toggle"); const nav = document.querySelector(".site-nav"); const links = document.querySelectorAll(".site-nav a"); const year = document.querySelector("#year"); const sections = document.querySelectorAll("main section[id]"); if (year) { year.textContent = String(new Date().getFullYear()); } window.addEventListener("scroll", () => { header.classList.toggle("is-scrolled", window.scrollY > 8); }); toggle.addEventListener("click", () => { const open = nav.classList.toggle("is-open"); toggle.setAttribute("aria-expanded", String(open)); }); links.forEach((link) => { link.addEventListener("click", () => { nav.classList.remove("is-open"); toggle.setAttribute("aria-expanded", "false"); }); }); const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (!entry.isIntersecting) { return; } const id = entry.target.getAttribute("id"); links.forEach((link) => { link.classList.toggle( "is-active", link.getAttribute("href") === `#${id}` ); }); }); }, { rootMargin: "-40% 0px -50% 0px", threshold: 0 } ); sections.forEach((section) => observer.observe(section));