This commit is contained in:
Scott González 2011-03-15 09:18:38 -04:00
commit 4881a27b82

View File

@ -18,60 +18,77 @@ $.effects.effect.explode = function( o ) {
var rows = o.pieces ? Math.round(Math.sqrt(o.pieces)) : 3, var rows = o.pieces ? Math.round(Math.sqrt(o.pieces)) : 3,
cells = rows, cells = rows,
el = $( this ).show().css( 'visibility', 'hidden' ), el = $( this ),
mode = $.effects.setMode( el, o.mode || 'hide' ), mode = $.effects.setMode( el, o.mode || 'hide' ),
offset = el.offset(), show = ( mode == 'show' ),
width = el.outerWidth( true ),
height = el.outerHeight( true ),
peices = [];
//Substract the margins - not fixing the problem yet. // show and then visibility:hidden the element before calculating offset
offset.top -= parseInt( el.css( "marginTop" ), 10 ) || 0; offset = el.show().css( 'visibility', 'hidden' ).offset(),
offset.left -= parseInt( el.css( "marginLeft" ), 10 ) || 0;
// width and height of a piece
width = Math.ceil( el.outerWidth() / cells ),
height = Math.ceil( el.outerHeight() / rows ),
pieces = [],
// loop
i, j, left, top, mx, my;
// clone the element for each row and cell. // clone the element for each row and cell.
for( var i = 0; i < rows ; i++ ) { // = for( i = 0; i < rows ; i++ ) { // ===>
for( var j = 0; j < cells ; j++ ) { // || top = offset.top + i * height;
my = i - ( rows - 1 ) / 2 ;
for( j = 0; j < cells ; j++ ) { // |||
left = offset.left + j * width;
mx = j - ( cells - 1 ) / 2 ;
// Create a clone of the now hidden main element that will be absolute positioned
// within a wrapper div off the -left and -top equal to size of our pieces
el el
.clone() .clone()
.appendTo('body') .appendTo( 'body' )
.wrap('<div></div>') .wrap( '<div></div>' )
.css({ .css({
position: 'absolute', position: 'absolute',
visibility: 'visible', visibility: 'visible',
left: -j*(width/cells), left: -j * width,
top: -i*(height/rows) top: -i * height
}) })
// select the wrapper - make it overflow: hidden and absolute positioned based on
// where the original was located +left and +top equal to the size of pieces
.parent() .parent()
.addClass('ui-effects-explode') .addClass( 'ui-effects-explode' )
.css({ .css({
position: 'absolute', position: 'absolute',
overflow: 'hidden', overflow: 'hidden',
width: width/cells, width: width,
height: height/rows, height: height,
left: offset.left + j*(width/cells) + (o.mode == 'show' ? (j-Math.floor(cells/2))*(width/cells) : 0), left: left + ( show ? mx * width : 0 ),
top: offset.top + i*(height/rows) + (o.mode == 'show' ? (i-Math.floor(rows/2))*(height/rows) : 0), top: top + ( show ? my * height : 0 ),
opacity: mode == 'show' ? 0 : 1 opacity: show ? 0 : 1
}).animate({ }).animate({
left: offset.left + j*(width/cells) + (o.mode == 'show' ? 0 : (j-Math.floor(cells/2))*(width/cells)), left: left + ( show ? 0 : mx * width ),
top: offset.top + i*(height/rows) + (o.mode == 'show' ? 0 : (i-Math.floor(rows/2))*(height/rows)), top: top + ( show ? 0 : my * height ),
opacity: mode == 'show' ? 1 : 0 opacity: show ? 1 : 0
}, o.duration || 500, childComplete ); }, o.duration || 500, o.easing, childComplete );
} }
} }
// children animate complete: // children animate complete:
function childComplete() { function childComplete() {
peices.push( this ); pieces.push( this );
if ( peices.length == rows * cells ) { if ( pieces.length == rows * cells ) {
animComplete(); animComplete();
} }
} }
function animComplete() { function animComplete() {
el.css({ visibility: 'visible' }); el.css({
$( peices ).remove(); visibility: 'visible'
if ( mode != 'show' ) { });
$( pieces ).remove();
if ( !show ) {
el.hide(); el.hide();
} }
if ( $.isFunction( o.complete ) ) { if ( $.isFunction( o.complete ) ) {