Skip to content

Instantly share code, notes, and snippets.

@thesandybridge
Last active April 7, 2022 16:29
Show Gist options
  • Save thesandybridge/b1c3399283564d68d44746939dd6bd02 to your computer and use it in GitHub Desktop.
Save thesandybridge/b1c3399283564d68d44746939dd6bd02 to your computer and use it in GitHub Desktop.
vertical scroll position
/**
* 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