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

100
Visualizações
ScrollSpy not working with dynamic height

I'm working on a single-page website, and am trying to implement a scroll-spy feature:

Here's what my navbar looks like:

<nav class="menu">
        <ul class="nav-links">
            <li><a href="#home">Home</a></li>
            <li><a href="#about">About Us</a></li>
            <li><a href="#aboutStem">What is STEM</a></li>
            <li><a href="#quotes">Products</a></li>
            <li><a href="#accomplishments">Contact Us</a></li>
        </ul>
    </nav>

In addition to this, I have created sections for each link in the navbar. The scrollspy is as follows:

document.addEventListener('DOMContentLoaded', () => {

const scrollSpy = () => {
    const targets = document.querySelectorAll('section');
    console.log(targets)
    const options = {
      threshold: 0.5,
    };

    if ('IntersectionObserver' in window) {
      const inView = target => {
        const interSecObs = new IntersectionObserver(entries => {
          entries.forEach(entry => {
            const elem = entry.target;
            const currentNav = document.querySelector(`[href='#${elem.id}']`);
            currentNav?.classList.toggle('active', entry.isIntersecting);
          })
        }, options);
        interSecObs.observe(target)
      };
      targets.forEach(inView);

    }
  };

  scrollSpy();

})

The scrollspy works fine as long as I specify the absolute height of a section, however, for responsiveness, I am using height: fit-content and that is causing errors.

Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
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