Last active
April 7, 2022 16:29
-
-
Save thesandybridge/b1c3399283564d68d44746939dd6bd02 to your computer and use it in GitHub Desktop.
vertical scroll position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns a number between 0 and 100 relative to scroll position. | |
* | |
* If target scroll container has top and bottom margins, results may be different | |
* than expected. | |
* @param {*} scrollContainer target scroll container, default is document.body. | |
* @returns number between 0 & 100. | |
*/ | |
function getVerticalScrollPercentage( scrollContainer = document.body ){ | |
const p = scrollContainer.parentNode; | |
const percentage = (scrollContainer.scrollTop || p.scrollTop) / (p.scrollHeight - p.clientHeight ) * 100; | |
return Math.round(percentage); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment