Last active
July 15, 2017 21:03
-
-
Save casprwang/9f9c8003fe129d495cb1d7c9d513dae2 to your computer and use it in GitHub Desktop.
logging the scrolled position with window.requestAnimationFrame
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
<style> | |
html { | |
height: 2000px; | |
} | |
</style> | |
</head> | |
<body> | |
lol | |
</body> | |
<script> | |
var last_known_scroll_position = 0; | |
var ticking = false; | |
function doSomething(scroll_pos) { | |
console.log( | |
last_known_scroll_position | |
) | |
} | |
window.addEventListener('scroll', function(e) { | |
last_known_scroll_position = window.scrollY; | |
if (!ticking) { | |
window.requestAnimationFrame(function() { | |
doSomething(last_known_scroll_position); | |
ticking = false; | |
}); | |
} | |
ticking = true; | |
}); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment