Created
November 26, 2018 08:54
-
-
Save kilfu0701/c3a5172d06b64e7221cc8b2eeb4927b4 to your computer and use it in GitHub Desktop.
Calculate the scroller percentage in html page.
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
// 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