A purely theoretical question if anyone is curious:

How is this effect implemented http://jqueryui.com/effect/#default (effect: Explode)?

Interests purely physically how can a holistic one, say a DIV which inside consists of elements (pictures, paragraphs and all sorts of lists) split into 9 parts? The only implementation that seems to me to remove the "screenshot" and split it into 9 different divs and then animate (sorry for the folly).

I picked the jQuery codes for the answer I did not find, if anyone has the time and knowledge - tell me, I will be grateful. Thank!

    2 answers 2

    In fact, cloned elements are created ( "pieces" ), each one has a fixed size with overflow: hidden; Each scatters in his own way. In general, everything is very simple and you can see it in firebug;)

    • I will get you up to 20k - ferrari

    If you read the source jQuery UI, you can guess:

    (function( $, undefined ) { $.effects.effect.explode = function( o, done ) { var rows = o.pieces ? Math.round( Math.sqrt( o.pieces ) ) : 3, cells = rows, el = $( this ), mode = $.effects.setMode( el, o.mode || "hide" ), show = mode === "show", // show and then visibility:hidden the element before calculating offset offset = el.show().css( "visibility", "hidden" ).offset(), // 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; // children animate complete: function childComplete() { pieces.push( this ); if ( pieces.length === rows * cells ) { animComplete(); } } // clone the element for each row and cell. for( i = 0; i < rows ; i++ ) { // ===> 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 .clone() .appendTo( "body" ) .wrap( "<div></div>" ) .css({ position: "absolute", visibility: "visible", left: -j * width, 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() .addClass( "ui-effects-explode" ) .css({ position: "absolute", overflow: "hidden", width: width, height: height, left: left + ( show ? mx * width : 0 ), top: top + ( show ? my * height : 0 ), opacity: show ? 0 : 1 }).animate({ left: left + ( show ? 0 : mx * width ), top: top + ( show ? 0 : my * height ), opacity: show ? 1 : 0 }, o.duration || 500, o.easing, childComplete ); } } function animComplete() { el.css({ visibility: "visible" }); $( pieces ).remove(); if ( !show ) { el.hide(); } done(); } }; })(jQuery); 

    Namely, we divide the necessary element into the required number of pieces (9 by default), each of which is a copy of the current one (clones), but with fixed values ​​of length and width, with overflow: hidden. We position the pieces absolutely and set the necessary indents, which are then animated according to a certain law. The original element is initially hidden, and at the end of the animation it becomes visible again. In fact, the code is well documented and, after reading the comments, you can easily figure it out.

    • one
      While reading the source / write the answer, ahead. - Gena Tsarinny
    • Yes, you have a very complete answer, thanks for your interest. It is even surprising how it works and does not slow down. I saw somewhere else as an example of how the picture was broken into 100 pieces and scattered on jQuery, unfortunately I could not find it. - ferrari
    • I myself was interested to learn how it works :) - Gena Tsarinny