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

282
Views
How to animate elements when it completely enters the viewport?

I made an animation to the h1 element using CSS, now I want the animation to occur when the h1 element comes into the viewport completely when the user scrolls down.

By the time the user scrolls down the page, the animation has already occurred.

I want the animation to occur only when the h1 element enters the viewport by adding the class .showtext to the span element.

Cannot set up a proper intersection observer, plz help

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.text {
    display: inline-block;
    overflow: hidden
}

.page1{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.page2{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.showtext{
  display: block;
  animation: reveal 1.5s cubic-bezier(0.77, 0, 0.175, 1) 0.5s;
}

@keyframes reveal {
  0% {
    transform: translate(0,100%);

  }
  100% {
    transform: translate(0,0);
  }
}
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>LNPD02</title>
    <link rel="stylesheet" href="styles.css" />
  </head>

  <body>

    <div class="page1">
      <h1>Scroll down</h1>
    </div>

    <div class="page2">
      <h1 class="text"><span class="hidetext showtext">Animated Text</span></h1>
    </div>

  </body>

</html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can check it with javascript. A function is triggered every time the user scrolls the page. If the element's position is greater than the screen's top and less than the screen's bottom that is in the viewport, toggle the classes to make the animaiton.

var text = document.querySelector('.text')
function scrolListener(e){
  var screenTop = document.scrollingElement.scrollTop;
  var screenBottom = screenTop + innerHeight;
  var textTop = text.getBoundingClientRect().top
  
  if ( textTop < screenBottom && textTop < screenTop)
  {
    text.children[0].classList.add("showtext");
    text.children[0].classList.remove("hidetext");
  }
  
}


document.onscroll = scrolListener
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.text {
  display: inline-block;
  overflow: hidden;
}

.page1 {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.page2 {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.text span {
  transition: .3s;
}


.showtext {
  display: block;
  animation: reveal 1.5s cubic-bezier(0.77, 0, 0.175, 1) 0.5s;
}
.hidetext {
  opacity: 0;
  transform: translate(0, 100%);
}


@keyframes reveal {
  0% {
    transform: translate(0, 100%);
  }
  100% {
    transform: translate(0, 0);
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>LNPD02</title>
  <link rel="stylesheet" href="styles.css" />
</head>

<body>

  <div class="page1">
    <h1>Scroll down</h1>
  </div>

  <div class="page2">
    <h1 class="text"><span class="hidetext">Animated Text</span></h1>
  </div>

</body>

</html>

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!