(function () { const scrollMenu = document.querySelector('.scroll-menu'); const links = scrollMenu ? scrollMenu.querySelectorAll('a[href^="#"]') : []; const getOffsetTop = (el) => { let offsetTop = 0; while (el) { offsetTop += el.offsetTop; el = el.offsetParent; } return offsetTop; }; const setActiveLink = () => { let fromTop = window.scrollY + 200; links.forEach(link => { let section = document.querySelector(link.getAttribute('href')); if ( section && getOffsetTop(section) <= fromTop && getOffsetTop(section) + section.offsetHeight > fromTop ) { link.classList.add('active'); } else { link.classList.remove('active'); } }); }; document.addEventListener('scroll', setActiveLink); window.addEventListener('load', setActiveLink); })();