Skip to content

Instantly share code, notes, and snippets.

@odil-io
Last active February 28, 2018 10:06
Show Gist options
  • Save odil-io/64ea2921402e062468650db7fffc5b8b to your computer and use it in GitHub Desktop.
Save odil-io/64ea2921402e062468650db7fffc5b8b to your computer and use it in GitHub Desktop.
JS: Vanilla Cross-Browser scroll position reporting
//https://stackoverflow.com/a/8028584
function getScrollPercent() {
var h = document.documentElement,
b = document.body,
st = 'scrollTop',
sh = 'scrollHeight';
return (h[st]||b[st]) / ((h[sh]||b[sh]) - h.clientHeight) * 100;
}
document.addEventListener('scroll', function(){
console.log( getScrollPercent() );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment