diff --git a/public/script.js b/public/script.js index 5fb0d3b..1b28a01 100644 --- a/public/script.js +++ b/public/script.js @@ -921,3 +921,44 @@ initThemeFilter(); } })(); + +// Active Section Highlighting for Navbar +(function () { + 'use strict'; + + function initScrollSpy() { + const sections = document.querySelectorAll('section[id]'); + const navLinks = document.querySelectorAll('.navbtn a.nav-link'); + + if (!sections.length || !navLinks.length) return; + + const observer = new IntersectionObserver( + function (entries) { + entries.forEach(function (entry) { + if (entry.isIntersecting) { + navLinks.forEach(function (link) { + link.classList.remove('active'); + if (link.getAttribute('href') === '#' + entry.target.id) { + link.classList.add('active'); + } + }); + } + }); + }, + { + rootMargin: '-80px 0px -50% 0px', + threshold: 0, + } + ); + + sections.forEach(function (section) { + observer.observe(section); + }); + } + + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', initScrollSpy); + } else { + initScrollSpy(); + } +})(); diff --git a/public/styles.css b/public/styles.css index 5415e79..1ce2d15 100644 --- a/public/styles.css +++ b/public/styles.css @@ -2299,4 +2299,4 @@ html { margin-top: var(--spacing-lg); padding-top: var(--spacing-lg); } -} \ No newline at end of file +}