effect-bounce.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*!
  2. * jQuery UI Effects Bounce 1.11.4
  3. * http://jqueryui.com
  4. *
  5. * Copyright jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * http://api.jqueryui.com/bounce-effect/
  10. */
  11. (function( factory ) {
  12. if ( typeof define === "function" && define.amd ) {
  13. // AMD. Register as an anonymous module.
  14. define([
  15. "jquery",
  16. "./effect"
  17. ], factory );
  18. } else {
  19. // Browser globals
  20. factory( jQuery );
  21. }
  22. }(function( $ ) {
  23. return $.effects.effect.bounce = function( o, done ) {
  24. var el = $( this ),
  25. props = [ "position", "top", "bottom", "left", "right", "height", "width" ],
  26. // defaults:
  27. mode = $.effects.setMode( el, o.mode || "effect" ),
  28. hide = mode === "hide",
  29. show = mode === "show",
  30. direction = o.direction || "up",
  31. distance = o.distance,
  32. times = o.times || 5,
  33. // number of internal animations
  34. anims = times * 2 + ( show || hide ? 1 : 0 ),
  35. speed = o.duration / anims,
  36. easing = o.easing,
  37. // utility:
  38. ref = ( direction === "up" || direction === "down" ) ? "top" : "left",
  39. motion = ( direction === "up" || direction === "left" ),
  40. i,
  41. upAnim,
  42. downAnim,
  43. // we will need to re-assemble the queue to stack our animations in place
  44. queue = el.queue(),
  45. queuelen = queue.length;
  46. // Avoid touching opacity to prevent clearType and PNG issues in IE
  47. if ( show || hide ) {
  48. props.push( "opacity" );
  49. }
  50. $.effects.save( el, props );
  51. el.show();
  52. $.effects.createWrapper( el ); // Create Wrapper
  53. // default distance for the BIGGEST bounce is the outer Distance / 3
  54. if ( !distance ) {
  55. distance = el[ ref === "top" ? "outerHeight" : "outerWidth" ]() / 3;
  56. }
  57. if ( show ) {
  58. downAnim = { opacity: 1 };
  59. downAnim[ ref ] = 0;
  60. // if we are showing, force opacity 0 and set the initial position
  61. // then do the "first" animation
  62. el.css( "opacity", 0 )
  63. .css( ref, motion ? -distance * 2 : distance * 2 )
  64. .animate( downAnim, speed, easing );
  65. }
  66. // start at the smallest distance if we are hiding
  67. if ( hide ) {
  68. distance = distance / Math.pow( 2, times - 1 );
  69. }
  70. downAnim = {};
  71. downAnim[ ref ] = 0;
  72. // Bounces up/down/left/right then back to 0 -- times * 2 animations happen here
  73. for ( i = 0; i < times; i++ ) {
  74. upAnim = {};
  75. upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance;
  76. el.animate( upAnim, speed, easing )
  77. .animate( downAnim, speed, easing );
  78. distance = hide ? distance * 2 : distance / 2;
  79. }
  80. // Last Bounce when Hiding
  81. if ( hide ) {
  82. upAnim = { opacity: 0 };
  83. upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance;
  84. el.animate( upAnim, speed, easing );
  85. }
  86. el.queue(function() {
  87. if ( hide ) {
  88. el.hide();
  89. }
  90. $.effects.restore( el, props );
  91. $.effects.removeWrapper( el );
  92. done();
  93. });
  94. // inject all the animations we just queued to be first in line (after "inprogress")
  95. if ( queuelen > 1) {
  96. queue.splice.apply( queue,
  97. [ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) );
  98. }
  99. el.dequeue();
  100. };
  101. }));