Skip to content

Instantly share code, notes, and snippets.

@hacfi
Last active August 29, 2015 14:27
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 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