effect-explode.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*!
  2. * jQuery UI Effects Explode 1.14.1
  3. * https://jqueryui.com
  4. *
  5. * Copyright OpenJS Foundation and other contributors
  6. * Released under the MIT license.
  7. * https://jquery.org/license
  8. */
  9. //>>label: Explode Effect
  10. //>>group: Effects
  11. /* eslint-disable max-len */
  12. //>>description: Explodes an element in all directions into n pieces. Implodes an element to its original wholeness.
  13. /* eslint-enable max-len */
  14. //>>docs: https://api.jqueryui.com/explode-effect/
  15. //>>demos: https://jqueryui.com/effect/
  16. ( function( factory ) {
  17. "use strict";
  18. if ( typeof define === "function" && define.amd ) {
  19. // AMD. Register as an anonymous module.
  20. define( [
  21. "jquery",
  22. "../version",
  23. "../effect"
  24. ], factory );
  25. } else {
  26. // Browser globals
  27. factory( jQuery );
  28. }
  29. } )( function( $ ) {
  30. "use strict";
  31. return $.effects.define( "explode", "hide", function( options, done ) {
  32. var i, j, left, top, mx, my,
  33. rows = options.pieces ? Math.round( Math.sqrt( options.pieces ) ) : 3,
  34. cells = rows,
  35. element = $( this ),
  36. mode = options.mode,
  37. show = mode === "show",
  38. // Show and then visibility:hidden the element before calculating offset
  39. offset = element.show().css( "visibility", "hidden" ).offset(),
  40. // Width and height of a piece
  41. width = Math.ceil( element.outerWidth() / cells ),
  42. height = Math.ceil( element.outerHeight() / rows ),
  43. pieces = [];
  44. // Children animate complete:
  45. function childComplete() {
  46. pieces.push( this );
  47. if ( pieces.length === rows * cells ) {
  48. animComplete();
  49. }
  50. }
  51. // Clone the element for each row and cell.
  52. for ( i = 0; i < rows; i++ ) { // ===>
  53. top = offset.top + i * height;
  54. my = i - ( rows - 1 ) / 2;
  55. for ( j = 0; j < cells; j++ ) { // |||
  56. left = offset.left + j * width;
  57. mx = j - ( cells - 1 ) / 2;
  58. // Create a clone of the now hidden main element that will be absolute positioned
  59. // within a wrapper div off the -left and -top equal to size of our pieces
  60. element
  61. .clone()
  62. .appendTo( "body" )
  63. .wrap( "<div></div>" )
  64. .css( {
  65. position: "absolute",
  66. visibility: "visible",
  67. left: -j * width,
  68. top: -i * height
  69. } )
  70. // Select the wrapper - make it overflow: hidden and absolute positioned based on
  71. // where the original was located +left and +top equal to the size of pieces
  72. .parent()
  73. .addClass( "ui-effects-explode" )
  74. .css( {
  75. position: "absolute",
  76. overflow: "hidden",
  77. width: width,
  78. height: height,
  79. left: left + ( show ? mx * width : 0 ),
  80. top: top + ( show ? my * height : 0 ),
  81. opacity: show ? 0 : 1
  82. } )
  83. .animate( {
  84. left: left + ( show ? 0 : mx * width ),
  85. top: top + ( show ? 0 : my * height ),
  86. opacity: show ? 1 : 0
  87. }, options.duration || 500, options.easing, childComplete );
  88. }
  89. }
  90. function animComplete() {
  91. element.css( {
  92. visibility: "visible"
  93. } );
  94. $( pieces ).remove();
  95. done();
  96. }
  97. } );
  98. } );