Answers for "active state of nav while scrolling"

0

active state of nav while scrolling

const links = document.querySelectorAll('.links');
const sections = document.querySelectorAll('section');

function changeLinkState() {
  let index = sections.length;

  while(--index && window.scrollY + 50 < sections[index].offsetTop) {}
  
  links.forEach((link) => link.classList.remove('active'));
  links[index].classList.add('active');
}

changeLinkState();
window.addEventListener('scroll', changeLinkState);
Posted by: Guest on August-31-2021

Code answers related to "active state of nav while scrolling"

Browse Popular Code Answers by Language