Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

354
Vistas
How to do animation on scroll in js on both ways (top/bottom)

I created a little aniamtion in JS by using bounding client. Once I scroll down until the text/content appears, I change its opacity to 1 by applying ".active" from CSS. And when I scroll up above the element again, opacity changes back to 0 (because ".active" gets taken away).

The problem is I want to make the same thing happen when I scroll up to the content element from below. Once the user goes below the content element, opacity should go to 0, then when they scroll back up (so the content element is again in view), opacity should go to 1. So it makes the animation work in both directions, something like on scrollrevealjs's front page.

document.addEventListener('scroll',()=>{
  let content = document.querySelector('.text');
  let contentPositiontop = content.getBoundingClientRect().top;
  let screenPosition = window.innerHeight ;
  if (contentPositiontop < screenPosition){   
    content.classList.add('active');                                                                
  }
  else{
    content.classList.remove('active');
  }
});

.text{
  transform: translateX(700px) translateY(1000px);
  font-family: Inter;
  font-weight: 800;
  font-size: 40px;
  opacity: 0;
  transition: all 2s ease;
  position: absolute;
}

.active{
  opacity: 1;
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You just need to check the height of the bottom of the content element as well as the top.

(For the top, we needed to add in the screen height (window.innerHeight) because we were comparing its position to the bottom of the screen. We don't need this for the bottom because we are comparing its position to the top of the screen, which has a vertical position of 0.)

When both the bottom and the top are in range, we show the content element.

(If the content element's height were greater than the height of the screen for some reason, you would have to choose values between 0 and window.innerHeight to use to trigger the transition.)

document.addEventListener('scroll',() => {
  const
    content = document.querySelector('.text'),
    top = content.getBoundingClientRect().top,
    bottom = content.getBoundingClientRect().bottom;
  if (top < innerHeight && bottom > 0){
    content.classList.add('active');
  }
  else{
    content.classList.remove('active');
  }
});
  .spacer{ height: 104vh; }
  .text{ height: 100vh; background: blue; transform: translateX(20px); opacity: 0; transition: all 2s ease; }
  .active{ opacity: 1; }
<div class="spacer"> </div>
<div class="text"></div>
<div class="spacer"> </div>
.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda