How to calculate the scroll percentage of a div in JavaScript

Viewed 23

I tried other questions but they didn't work as I wanted.

I want to get the scroll percentage of an element (let's say a div) to animate it (e.g. if the user can see the element in full the function would return 100% / 1.0).

How can I achieve this?

1 Answers

You could achieve it with the below formula,

var scrollPercent = (CurrentScrollPosition / (GivenTotalScrollHeightOfTheElement - ContentPostHeight)) * 100;
Related