Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

353
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!