Skip to content

Instantly share code, notes, and snippets.

@jmather
Created July 24, 2012 13:25
Show Gist options
  • Save jmather/3169901 to your computer and use it in GitHub Desktop.
Save jmather/3169901 to your computer and use it in GitHub Desktop.
Update to make AppendAround more efficient
jQuery(function( $ ){
var $$resize_callback_set = false;
var $$timer = null;
var $$callbacks = [];
$.fn.appendAround = function(opts){
return this.each(function(){
var $self = $( this ),
att = "data-set",
$set = $( "["+ att +"='"+ $self.closest( "["+ att +"]" ).attr( att ) + "']" );
function appendToVisibleContainer(){
if( $self.is( ":hidden" ) ){
$self.appendTo( $set.filter( ":visible:eq(0)" ) );
}
}
appendToVisibleContainer();
addResizeListener(appendToVisibleContainer);
if ($$resize_callback_set == false)
{
$(window).resize( (opts && opts.buffer == true) ? bufferResize : callResizeListeners );
$$resize_callback_set = true;
}
});
};
// As resize is called constantly, it is likely a good idea to add a bit
// of a buffer. Paul Irish does a good job summarizing the issue:
// http://paulirish.com/2009/throttled-smartresize-jquery-event-handler/
function bufferResize() {
if ($$timer != null)
clearTimeout($$timer);
$$timer = setTimeout(callResizeListeners, 100)
}
function callResizeListeners()
{
for (var i in $$callbacks)
$$callbacks[i]();
}
function addResizeListener(listener)
{
$$callbacks[$$callbacks.length] = listener;
}
});
@jmather
Copy link
Author

jmather commented Jul 24, 2012

Had a small bug of not calling callResizeListeners when not running in buffering mode.

Bug squashed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment