Skip to content

Instantly share code, notes, and snippets.

@cladley
Created August 12, 2015 10:09
Show Gist options
  • Select an option

  • Save cladley/9cfcff27a3374eec2b2f to your computer and use it in GitHub Desktop.

Select an option

Save cladley/9cfcff27a3374eec2b2f to your computer and use it in GitHub Desktop.
var latestKnownScrollY = 0,
ticking = false;
function onScroll(){
latestKnownScrollY = window.scrollY;
requestTick();
}
function requestTick(){
if(!ticking){
requestAnimationFrame(update);
}
ticking = true;
}
function update(){
// reset the tick so we can
// capture the next onScroll
ticking = false;
var currentScrollY = latestKnownScrollY;
// console.log("current Scroll : " + currentScrollY);
console.log(`current scroll ${currentScrollY}`);
// do whatever dom updates. Read first in a batch
// then write in a batch if possible
}
window.addEventListener('scroll', onScroll, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment