Last active
March 4, 2016 10:37
-
-
Save juice49/803496e06924be2cfab2 to your computer and use it in GitHub Desktop.
AppendAround with onRemove and onAppend callbacks
This file contains hidden or 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
/* | |
* !!! IMPORTANT !!! | |
* This has been modified to accept "onRemove" and "onAppend" callbacks. | |
* See comments 1, 2, and 3. | |
*/ | |
/*! 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({ onRemove = Function(), onAppend = Function() } = {}){ // [1] | |
return this.each(function(){ | |
var $self = $( this ), | |
att = "data-set", | |
$parent = $self.parent(), | |
parent = $parent[ 0 ], | |
attval = $parent.attr( att ), | |
$set = $( "["+ att +"='" + attval + "']" ); | |
function isHidden( elem ){ | |
return $(elem).css( "display" ) === "none"; | |
} | |
function appendToVisibleContainer(){ | |
if( isHidden( parent ) ){ | |
var found = 0; | |
onRemove($parent); // [2] | |
$set.each(function(){ | |
if( !isHidden( this ) && !found ){ | |
$self.appendTo( this ); | |
found++; | |
parent = this; | |
onAppend($(this)); // [3] | |
} | |
}); | |
} | |
} | |
appendToVisibleContainer(); | |
$(window).bind( "resize", appendToVisibleContainer ); | |
}); | |
}; | |
}( jQuery )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment