Skip to content

Instantly share code, notes, and snippets.

@hacfi
Last active August 29, 2015 14:27
Show Gist options
  • Save hacfi/017cd680f514b4af4c36 to your computer and use it in GitHub Desktop.
Save hacfi/017cd680f514b4af4c36 to your computer and use it in GitHub Desktop.
/*
* smartscroll: debounced scroll event for jQuery *
* based on smartresize by @louis_remi: https://github.com/lrbabe/jquery.smartresize.js *
* Copyright 2011 Louis-Remi & lukeshumard * Licensed under the MIT license. *
*/
var event = $.event,
scrollTimeout;
event.special.smartscroll = {
setup: function() {
$(this).bind( "scroll", event.special.smartscroll.handler );
},
teardown: function() {
$(this).unbind( "scroll", event.special.smartscroll.handler );
},
handler: function( event, execAsap ) {
// Save the context
var context = this,
args = arguments;
// set correct event type
event.type = "smartscroll";
if (scrollTimeout) { clearTimeout(scrollTimeout); }
scrollTimeout = setTimeout(function() {
jQuery.event.handle.apply( context, args );
}, execAsap === "execAsap"? 0 : 100);
}
};
$.fn.smartscroll = function( fn ) {
return fn ? this.bind( "smartscroll", fn ) : this.trigger( "smartscroll", ["execAsap"] );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment