Skip to content

Commit 39dcad6

Browse files
committed
effects.explode: rework math used to split the div into peices - split the box disregarding margins
Fixes #3968 - effects explode: explodes from the right instead of from the center
1 parent 3f8c608 commit 39dcad6

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

ui/jquery.effects.explode.js

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,62 @@ $.effects.explode = function( o ) {
1818

1919
var rows = o.pieces ? Math.round(Math.sqrt(o.pieces)) : 3,
2020
cells = rows,
21-
el = $( this ).show().css( 'visibility', 'hidden' ),
21+
el = $( this ),
2222
mode = $.effects.setMode( el, o.mode || 'hide' ),
23-
offset = el.offset(),
24-
width = el.outerWidth( true ),
25-
height = el.outerHeight( true ),
26-
peices = [];
23+
show = ( mode == 'show' ),
2724

28-
//Substract the margins - not fixing the problem yet.
29-
offset.top -= parseInt( el.css( "marginTop" ), 10 ) || 0;
30-
offset.left -= parseInt( el.css( "marginLeft" ), 10 ) || 0;
25+
// show and then visibility:hidden the element before calculating offset
26+
offset = el.show().css( 'visibility', 'hidden' ).offset(),
27+
28+
// width and height of a piece
29+
width = Math.ceil( el.outerWidth() / cells ),
30+
height = Math.ceil( el.outerHeight() / rows ),
31+
peices = [],
32+
i, j, pos;
3133

3234
// clone the element for each row and cell.
33-
for( var i = 0; i < rows ; i++ ) { // =
34-
for( var j = 0; j < cells ; j++ ) { // ||
35+
for( i = 0; i < rows ; i++ ) { // ===>
36+
for( j = 0; j < cells ; j++ ) { // |||
37+
pos = {
38+
// wrapper base position in body
39+
left: offset.left + j * width,
40+
top: offset.top + i * height,
41+
42+
// x position in matrix with 0,0 at the center
43+
rx: j - cells / 2,
44+
ry: i - rows / 2
45+
};
46+
47+
// Create a clone of the now hidden main element that will be absolute positioned
48+
// within a wrapper div off the -left and -top equal to size of our pieces
3549
el
3650
.clone()
37-
.appendTo('body')
38-
.wrap('<div></div>')
51+
.appendTo( 'body' )
52+
.wrap( '<div></div>' )
3953
.css({
4054
position: 'absolute',
4155
visibility: 'visible',
42-
left: -j*(width/cells),
43-
top: -i*(height/rows)
56+
left: -j * width,
57+
top: -i * height
4458
})
59+
60+
// select the wrapper - make it overflow: hidden and absolute positioned based on
61+
// where the original was located +left and +top equal to the size of pieces
4562
.parent()
46-
.addClass('ui-effects-explode')
63+
.addClass( 'ui-effects-explode' )
4764
.css({
4865
position: 'absolute',
4966
overflow: 'hidden',
50-
width: width/cells,
51-
height: height/rows,
52-
left: offset.left + j*(width/cells) + (o.mode == 'show' ? (j-Math.floor(cells/2))*(width/cells) : 0),
53-
top: offset.top + i*(height/rows) + (o.mode == 'show' ? (i-Math.floor(rows/2))*(height/rows) : 0),
54-
opacity: mode == 'show' ? 0 : 1
67+
width: width,
68+
height: height,
69+
left: pos.left + ( show ? pos.rx * width : 0 ),
70+
top: pos.top + ( show ? pos.ry * height : 0 ),
71+
opacity: show ? 0 : 1
5572
}).animate({
56-
left: offset.left + j*(width/cells) + (o.mode == 'show' ? 0 : (j-Math.floor(cells/2))*(width/cells)),
57-
top: offset.top + i*(height/rows) + (o.mode == 'show' ? 0 : (i-Math.floor(rows/2))*(height/rows)),
58-
opacity: mode == 'show' ? 1 : 0
59-
}, o.duration || 500, childComplete );
73+
left: pos.left + ( show ? 0 : pos.rx * width ),
74+
top: pos.top + ( show ? 0 : pos.ry * height ),
75+
opacity: show ? 1 : 0
76+
}, o.duration || 500, o.easing, childComplete );
6077
}
6178
}
6279

@@ -69,9 +86,11 @@ $.effects.explode = function( o ) {
6986
}
7087

7188
function animComplete() {
72-
el.css({ visibility: 'visible' });
89+
el.css({
90+
visibility: 'visible'
91+
});
7392
$( peices ).remove();
74-
if ( mode != 'show' ) {
93+
if ( !show ) {
7594
el.hide();
7695
}
7796
if ( $.isFunction( o.complete ) ) {

0 commit comments

Comments
 (0)