effect-bounce.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*!
  2. * jQuery UI Effects Bounce 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: Bounce Effect
  10. //>>group: Effects
  11. //>>description: Bounces an element horizontally or vertically n times.
  12. //>>docs: https://api.jqueryui.com/bounce-effect/
  13. //>>demos: https://jqueryui.com/effect/
  14. ( function( factory ) {
  15. "use strict";
  16. if ( typeof define === "function" && define.amd ) {
  17. // AMD. Register as an anonymous module.
  18. define( [
  19. "jquery",
  20. "../version",
  21. "../effect"
  22. ], factory );
  23. } else {
  24. // Browser globals
  25. factory( jQuery );
  26. }
  27. } )( function( $ ) {
  28. "use strict";
  29. return $.effects.define( "bounce", function( options, done ) {
  30. var upAnim, downAnim, refValue,
  31. element = $( this ),
  32. // Defaults:
  33. mode = options.mode,
  34. hide = mode === "hide",
  35. show = mode === "show",
  36. direction = options.direction || "up",
  37. distance = options.distance,
  38. times = options.times || 5,
  39. // Number of internal animations
  40. anims = times * 2 + ( show || hide ? 1 : 0 ),
  41. speed = options.duration / anims,
  42. easing = options.easing,
  43. // Utility:
  44. ref = ( direction === "up" || direction === "down" ) ? "top" : "left",
  45. motion = ( direction === "up" || direction === "left" ),
  46. i = 0,
  47. queuelen = element.queue().length;
  48. $.effects.createPlaceholder( element );
  49. refValue = element.css( ref );
  50. // Default distance for the BIGGEST bounce is the outer Distance / 3
  51. if ( !distance ) {
  52. distance = element[ ref === "top" ? "outerHeight" : "outerWidth" ]() / 3;
  53. }
  54. if ( show ) {
  55. downAnim = { opacity: 1 };
  56. downAnim[ ref ] = refValue;
  57. // If we are showing, force opacity 0 and set the initial position
  58. // then do the "first" animation
  59. element
  60. .css( "opacity", 0 )
  61. .css( ref, motion ? -distance * 2 : distance * 2 )
  62. .animate( downAnim, speed, easing );
  63. }
  64. // Start at the smallest distance if we are hiding
  65. if ( hide ) {
  66. distance = distance / Math.pow( 2, times - 1 );
  67. }
  68. downAnim = {};
  69. downAnim[ ref ] = refValue;
  70. // Bounces up/down/left/right then back to 0 -- times * 2 animations happen here
  71. for ( ; i < times; i++ ) {
  72. upAnim = {};
  73. upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance;
  74. element
  75. .animate( upAnim, speed, easing )
  76. .animate( downAnim, speed, easing );
  77. distance = hide ? distance * 2 : distance / 2;
  78. }
  79. // Last Bounce when Hiding
  80. if ( hide ) {
  81. upAnim = { opacity: 0 };
  82. upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance;
  83. element.animate( upAnim, speed, easing );
  84. }
  85. element.queue( done );
  86. $.effects.unshift( element, queuelen, anims + 1 );
  87. } );
  88. } );