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

142
Visualizações
Active class not appearing for sections (DOM manipulation)

The CSS and HTML elements build different sections of this landing page, which I am manipulating from the DOM. I am building a code to highlight the section currently in the viewport along with highlighting the nav element in view using Javascript. I did that by getting dimensions via getBoundingClientRect() and then add "active-class" using classlist. However, the active section is not working on my project. I already tried debugging the project.

When I scroll, a section in view does not get highlighted.

The project repository is located here: https://github.com/Himanshukumar30/Landing-Page

  // Check the section in view
    
    function sectionInView () {
        const position = element.getBoundingClientRect();
        return (position.top <= 150 && position.bottom >= 150);
    }
    
    // Set sections as active
    
    function activeClass() {
     for (section of sections) {
       if (sectionInView(section)) {
    
            // Add "active-class" to section in view port
                section.classList.add("active-class");
                
            } else {
    
                // Remove "active-class" from section not in view port
                section.classList.remove("active-class");
            }
        }
    }
    
    // Function call to show active section in view
    document.addEventListener("scroll", function() {
        activeClass();
      });

I tried adding active-class manually, it still didn't work.

When I log section in console, it gives following error:

63app.js:32 Uncaught ReferenceError: element is not defined
at sectionInView (app.js:32:22)
at activeClass (app.js:39:13)
at HTMLDocument.<anonymous> (app.js:54:5)
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