Created
September 1, 2017 20:41
-
-
Save bradoyler/6037548a8c132184476fb5374c327da7 to your computer and use it in GitHub Desktop.
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
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