Skip to content

Instantly share code, notes, and snippets.

@casprwang
Last active July 15, 2017 21:03
Show Gist options
  • Save casprwang/9f9c8003fe129d495cb1d7c9d513dae2 to your computer and use it in GitHub Desktop.
Save casprwang/9f9c8003fe129d495cb1d7c9d513dae2 to your computer and use it in GitHub Desktop.
logging the scrolled position with window.requestAnimationFrame
<!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