Skip to content

Instantly share code, notes, and snippets.

@sp90
Created April 20, 2016 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sp90/57097bf18b5e3cfe7739b0fc9ddf2c2a to your computer and use it in GitHub Desktop.
Save sp90/57097bf18b5e3cfe7739b0fc9ddf2c2a to your computer and use it in GitHub Desktop.
function getScrollPosition() {
var last_known_scroll_position = 0;
var ticking = false;
var bodyEl = document.body;
var htmlEl = document.documentElement;
var height = Math.max(bodyEl.scrollHeight, bodyEl.offsetHeight,
htmlEl.clientHeight, htmlEl.scrollHeight, htmlEl.offsetHeight);
window.addEventListener('scroll', function(e) {
last_known_scroll_position = window.scrollY;
if (!ticking) {
window.requestAnimationFrame(function() {
onScroll(last_known_scroll_position);
ticking = false;
});
}
ticking = true;
});
function onScroll(yPos) {
console.log(yPos);
console.log(height);
console.log((yPos / height) * 100);
}
};
getScrollPosition();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment