Created
July 24, 2012 20:04
-
-
Save scottjehl/3172276 to your computer and use it in GitHub Desktop.
appendaround with simple debounce
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
/*! 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