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

151
Visualizações
Remove ("active") when not visible anymore and add ("active") when visible again but this also from the bottom instead of just top

Hey guys my brain is afk right now so I am asking yall:

I am trying to make a reveal animation everytime the element is visible. At the moment this only works when you scroll down and make the element visible from the top. I want to extend this also for scrolling up and making the element visible from the bottom.

Can anyone please explain what I need to change in order to accomplish that?

JAVASCRIPT:

function reveal() {
  var reveals = document.querySelectorAll(".reveal");
  for (var i = 0; i < reveals.length; i++) {
    var windowHeight = window.innerHeight;
    var elementTop = reveals[i].getBoundingClientRect().top;
    var elementVisible = 100;
    var elementHidden = ;
                        
    if (elementTop < windowHeight - elementVisible) {
    reveals[i].classList.add("active");
    }
                        
    else {
    reveals[i].classList.remove("active");
    }
  }
}

window.addEventListener("scroll", reveal);
reveal();

CSS:

.reveal{
    transform: translateY(SOMEVALUEpx);
    opacity: 0;
}

.reveal.active{
    transform: translateY(0);
    opacity: 1;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add another check in your if statement to see if the element bottom has reached height 0 plus the elementVisible value.

function reveal() {
  var reveals = document.querySelectorAll(".reveal");
  var windowHeight = window.innerHeight;
  var elementVisible = 100;

  for (var i = 0; i < reveals.length; i++) {
    var elementTop = reveals[i].getBoundingClientRect().top;
    var elementBottom = reveals[i].getBoundingClientRect().bottom;
                        
    if (elementTop < windowHeight - elementVisible || elementBottom > 0 + elementVisible) {
      reveals[i].classList.add("active");
    } else {
      reveals[i].classList.remove("active");
    }
  }
}

window.addEventListener("scroll", reveal);
reveal();

I also defined windowHeight and elementVisible outside the loop, as they don't need to be redefined for each iteration of the loop. All this happens in one single scroll.

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