📜  滚动时导航的活动状态 - 无论代码示例

📅  最后修改于: 2022-03-11 14:57:42.445000             🧑  作者: Mango

代码示例1
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);