Created
June 11, 2014 14:25
-
-
Save rutger1140/691c865154736508332e to your computer and use it in GitHub Desktop.
Debouncing scroll events
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
// Sample code from Paul Lewis | |
// Source: http://www.html5rocks.com/en/tutorials/speed/animations/ | |
var latestKnownScrollY = 0, | |
ticking = false; | |
function onScroll() { | |
latestKnownScrollY = window.scrollY; | |
requestTick(); | |
} | |
function requestTick() { | |
if(!ticking) { | |
requestAnimationFrame(update); | |
} | |
ticking = true; | |
} | |
function update() { | |
ticking = false; | |
var currentScrollY = latestKnownScrollY; | |
console.log("Update!"); | |
} | |
window.addEventListener('scroll', onScroll, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment