39 lines
1.5 KiB
JavaScript
39 lines
1.5 KiB
JavaScript
// Navigation progressively enhances normal links; content works without JavaScript.
|
|
(() => {
|
|
const toggle = document.querySelector('.nav-toggle');
|
|
const nav = document.querySelector('.site-nav');
|
|
const closeMenu = () => {
|
|
nav?.classList.remove('open');
|
|
toggle?.setAttribute('aria-expanded', 'false');
|
|
};
|
|
toggle?.addEventListener('click', () => {
|
|
const open = nav.classList.toggle('open');
|
|
toggle.setAttribute('aria-expanded', String(open));
|
|
});
|
|
nav?.addEventListener('click', event => {
|
|
if (event.target.closest('a')) closeMenu();
|
|
});
|
|
document.addEventListener('keydown', event => {
|
|
if (event.key === 'Escape' && nav?.classList.contains('open')) {
|
|
closeMenu();
|
|
toggle.focus();
|
|
}
|
|
});
|
|
if ('IntersectionObserver' in window) {
|
|
const observer = new IntersectionObserver(entries => {
|
|
const visible = entries.filter(entry => entry.isIntersecting);
|
|
if (!visible.length) return;
|
|
const id = visible[0].target.id;
|
|
nav?.querySelectorAll('a[href^="#"]').forEach(link => {
|
|
const active = link.getAttribute('href') === `#${id}`;
|
|
link.classList.toggle('active', active);
|
|
if (active) link.setAttribute('aria-current', 'location');
|
|
else link.removeAttribute('aria-current');
|
|
});
|
|
}, { rootMargin: '-10% 0px -55% 0px', threshold: 0 });
|
|
document.querySelectorAll('main section[id]').forEach(section => observer.observe(section));
|
|
}
|
|
const year = document.getElementById('year');
|
|
if (year) year.textContent = new Date().getFullYear();
|
|
})();
|