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

96
Visualizações
My active class is overriding my scroll to top function in JavaScript

I have coded a smooth scroll to top button JS shown here:

mybutton = document.getElementById("btnTop");
// When the user scrolls down 20px from the top of the document, show the button. 
// This can be modified!
window.onscroll = function() {scrollFunction()};

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.style.display = "block";
  } else {
    mybutton.style.display = "none";
  }
}

// When the user clicks on the button, scroll to the top of the document
function topFunction() {
    //   document.body.scrollTop = 0; // For Safari
    //   document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
    window.scrollTo({ top: 0, behavior: 'smooth' });
    }

with the rest of the code being CSS for visuals and an HTML callout and it was working just fine until I had to use a getBoundingClientRect to get the DOM to detect the active state of the current section on the page. Code Shown here

window.onscroll = function() {
      document.querySelectorAll("section").forEach(function(active) {
        let activeLink = navBar.querySelector(`[data-nav=${active.id}]`);
      if(active.getBoundingClientRect().top >= -400 && active.getBoundingClientRect().top <= 150){
    
        active.classList.add("your-active-class");
        activeLink.classList.add("active-link");
    
        }
        else{
             active.classList.remove("your-active-class");
             activeLink.classList.remove("active-link");
        }
      });
    }

and since then my button disappeared, i would say it has something to do with the window.onscroll function. how do i fix this while keeping both codes?

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