Skip to content

Instantly share code, notes, and snippets.

@danyeah
Last active September 21, 2015 14:25
Show Gist options
  • Save danyeah/5b9103de48430df26ab4 to your computer and use it in GitHub Desktop.
Save danyeah/5b9103de48430df26ab4 to your computer and use it in GitHub Desktop.
// 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