Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
Make navbar link active on wheel event rather than scroll?

I want to make the links in my navbar have an active class when you scroll into the corresponding section.

The code below was working just fine until I implemented a smooth scroll/parallax library which removes the scroll event.

I tried making the code work using the wheel event, tried seeing if there was anything similar to scrollY for wheel events but I couldn't find anything.

Any ideas on how I could implement this feature? It can be different from the implementation I had before

EDIT: Here's a codepen. If you uncomment the locomotive portion, the code no longer works. How can I make it work?

const sections = document.querySelectorAll("section");
const navLinks = document.querySelectorAll("nav a");

window.addEventListener("scroll", function () {
  let navbar = document.querySelector("nav");
  let current = "";
  sections.forEach(function (section) {
    const sectionTop = section.offsetTop;
    const sectionHeight = section.clientHeight;
    if (scrollY >= sectionTop - sectionHeight / 3) {
      current = `#${section.getAttribute("id")}`;
    }
    navLinks.forEach(function (each) {
      // add/remove active class
      each.classList.remove("nav-active");
      if (each.getAttribute("href") == current) {
        each.classList.add("nav-active");
      }
    });
  });
});

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try using an IntersectionObserver instead:

const navLinks = document.querySelectorAll("nav a");

const updateNav = (entries, observer) => {
    const matchingIds = entries.filter(e => e.isIntersecting).map(e => `#${e.target.id}`);
    
    if (matchingIds.length !== 0) {
        const current = matchingIds[0];
        navLinks.forEach(function(link) {
            link.classList.remove("nav-active");
            if (link.getAttribute("href") == current) {
                link.classList.add("nav-active");
            }
        });
    }
};

const options = {
    root: null,
    rootMargin: "0px",
    threshold: 0.66
}

const observer = new IntersectionObserver(updateNav, options);
document.querySelectorAll("*[data-scroll-section]").forEach(el => observer.observe(el));

Intersection Observer API
Demo

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda