Last active
September 21, 2015 14:25
-
-
Save danyeah/5b9103de48430df26ab4 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
// Position fixed: pattern: https://github.com/shichuan/javascript-patterns/blob/master/jquery-patterns/window-scroll-event.html#L36 | |
// RequestAnimationFrame Shim | |
window.animFrame = (function () { | |
return window.requestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
window.mozRequestAnimationFrame || | |
function (callback) { | |
window.setTimeout(callback, 15); | |
}; | |
})(); | |
var scrollTimeout, | |
$target = $('.target'), | |
targetOffset = $target.offset(); | |
$(window).scroll(function () { | |
if (scrollTimeout) { | |
// clear the timeout, if one is pending | |
// TODO: cerca come si cancella un requestAnimationFrame | |
clearTimeout(scrollTimeout); | |
scrollTimeout = null; | |
} | |
scrollTimeout = animFrame(scrollHandler, 15); | |
}); | |
scrollHandler = function () { | |
if ($(window).scrollTop() > targetOffset.top) { | |
//do something with $target | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment