Skip to content

Instantly share code, notes, and snippets.

@kilfu0701
Created November 26, 2018 08:54
Show Gist options
  • Save kilfu0701/c3a5172d06b64e7221cc8b2eeb4927b4 to your computer and use it in GitHub Desktop.
Save kilfu0701/c3a5172d06b64e7221cc8b2eeb4927b4 to your computer and use it in GitHub Desktop.
Calculate the scroller percentage in html page.
// you can paste this code into chrome console, then check the result.
const pageHeight = (function () {
return Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight,
document.body.clientHeight,
document.documentElement.clientHeight
);
})();
document.onscroll = (e) => {
const top = e.target.scrollingElement.scrollTop,
h = e.target.scrollingElement.clientHeight;
console.log( Math.round( top / (pageHeight-h) * 100) );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment