effect-shake.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*!
  2. * jQuery UI Effects Shake 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: Shake Effect
  10. //>>group: Effects
  11. //>>description: Shakes an element horizontally or vertically n times.
  12. //>>docs: https://api.jqueryui.com/shake-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( "shake", function( options, done ) {
  30. var i = 1,
  31. element = $( this ),
  32. direction = options.direction || "left",
  33. distance = options.distance || 20,
  34. times = options.times || 3,
  35. anims = times * 2 + 1,
  36. speed = Math.round( options.duration / anims ),
  37. ref = ( direction === "up" || direction === "down" ) ? "top" : "left",
  38. positiveMotion = ( direction === "up" || direction === "left" ),
  39. animation = {},
  40. animation1 = {},
  41. animation2 = {},
  42. queuelen = element.queue().length;
  43. $.effects.createPlaceholder( element );
  44. // Animation
  45. animation[ ref ] = ( positiveMotion ? "-=" : "+=" ) + distance;
  46. animation1[ ref ] = ( positiveMotion ? "+=" : "-=" ) + distance * 2;
  47. animation2[ ref ] = ( positiveMotion ? "-=" : "+=" ) + distance * 2;
  48. // Animate
  49. element.animate( animation, speed, options.easing );
  50. // Shakes
  51. for ( ; i < times; i++ ) {
  52. element
  53. .animate( animation1, speed, options.easing )
  54. .animate( animation2, speed, options.easing );
  55. }
  56. element
  57. .animate( animation1, speed, options.easing )
  58. .animate( animation, speed / 2, options.easing )
  59. .queue( done );
  60. $.effects.unshift( element, queuelen, anims + 1 );
  61. } );
  62. } );