Skip to content

Instantly share code, notes, and snippets.

@scottjehl
Created July 24, 2012 20:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottjehl/3172276 to your computer and use it in GitHub Desktop.
Save scottjehl/3172276 to your computer and use it in GitHub Desktop.
appendaround with simple debounce
/*! appendAround markup pattern. [c]2012, @scottjehl, Filament Group, Inc. MIT/GPL
how-to:
1. Insert potential element containers throughout the DOM
2. give each container a data-set attribute with a value that matches all other containers' values
3. Place your appendAround content in one of the potential containers
4. Call appendAround() on that element when the DOM is ready
*/
(function( $ ){
$.fn.appendAround = function(){
return this.each(function(){
var $self = $( this ),
att = "data-set",
$set = $( "["+ att +"='"+ $self.closest( "["+ att +"]" ).attr( att ) + "']" ),
debounce;
function appendToVisibleContainer(){
if( $self.is( ":hidden" ) ){
$self.appendTo( $set.filter( ":visible:eq(0)" ) );
}
}
appendToVisibleContainer();
$(window).resize( function(){
if( debounce ){
clearTimeout( debounce );
}
debounce = setTimeout( appendToVisibleContainer, 100 );
} );
});
};
}( jQuery ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment