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

166
Views
How can I apply scroll animation before top of viewport + calculate proper percentage?

I am trying to animate a path incrementally on scroll. I am following the techniques outlined here and here. I have my animation areas broken up into segments, and I am using waypoints to activate each segment. once activated (when the top of the segment hits the top of the viewport), the segment listens for the scroll event and then calculates the percentage of line to animate based on this snippet:

var scrollPercentage = rect.top / rect.height;

This seems to work ok, codepen is here. however I need this animation to start prior to the segment reaching the top of the viewport, say when it is 100px from the top. Also I need a way to calculate the percentage when rect.top is between 0 and 100. For some reason when I add a conditional statement it either a. doesn't fire or b. draws the path backwards:

    if (rect.top > 0 && rect.top < 100) {
      //unsure what how to calulate and trigger before
      //top of viewport
      //var scrollPercentage = ?;
    } else if (rect.top < 0 && rect.bottom > 0) {
      var scrollPercentage = rect.top / rect.height;
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You just need to add an offset to the rect top and bottom values. Then use that value in your comparison.

I've called it prestart.

var prestart = 100;
if ((rect.top - prestart) < 0 && (rect.bottom - prestart) > 0) {
  var scrollPercentage = (rect.top - prestart) / rect.height;
}
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!