Created
April 20, 2016 14:37
-
-
Save sp90/57097bf18b5e3cfe7739b0fc9ddf2c2a to your computer and use it in GitHub Desktop.
This file contains 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
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