Created
July 24, 2012 13:25
-
-
Save jmather/3169901 to your computer and use it in GitHub Desktop.
Update to make AppendAround more efficient
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
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; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Had a small bug of not calling callResizeListeners when not running in buffering mode.
Bug squashed.