jquery.ui.effect-bounce.js 2.7KB

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