Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Created September 1, 2017 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradoyler/6037548a8c132184476fb5374c327da7 to your computer and use it in GitHub Desktop.
Save bradoyler/6037548a8c132184476fb5374c327da7 to your computer and use it in GitHub Desktop.
function rAFscroll (fn) {
const rAF = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
// IE Fallback, you can even fallback to onscroll
function (callback) { window.setTimeout(callback, 1000 / 60) }
var lastPosition = -1
function loop () {
// Avoid calculations if not needed
if (lastPosition === window.pageYOffset) {
rAF(loop)
return false
} else lastPosition = window.pageYOffset
fn()
rAF(loop)
}
loop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment