暫無描述

effect-shake.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*!
  2. * jQuery UI Effects Shake 1.12.1
  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. //>>label: Shake Effect
  10. //>>group: Effects
  11. //>>description: Shakes an element horizontally or vertically n times.
  12. //>>docs: http://api.jqueryui.com/shake-effect/
  13. //>>demos: http://jqueryui.com/effect/
  14. ( function( factory ) {
  15. if ( typeof define === "function" && define.amd ) {
  16. // AMD. Register as an anonymous module.
  17. define( [
  18. "jquery",
  19. "./effect"
  20. ], factory );
  21. } else {
  22. // Browser globals
  23. factory( jQuery );
  24. }
  25. }( function( $ ) {
  26. return $.effects.define( "shake", function( options, done ) {
  27. var i = 1,
  28. element = $( this ),
  29. direction = options.direction || "left",
  30. distance = options.distance || 20,
  31. times = options.times || 3,
  32. anims = times * 2 + 1,
  33. speed = Math.round( options.duration / anims ),
  34. ref = ( direction === "up" || direction === "down" ) ? "top" : "left",
  35. positiveMotion = ( direction === "up" || direction === "left" ),
  36. animation = {},
  37. animation1 = {},
  38. animation2 = {},
  39. queuelen = element.queue().length;
  40. $.effects.createPlaceholder( element );
  41. // Animation
  42. animation[ ref ] = ( positiveMotion ? "-=" : "+=" ) + distance;
  43. animation1[ ref ] = ( positiveMotion ? "+=" : "-=" ) + distance * 2;
  44. animation2[ ref ] = ( positiveMotion ? "-=" : "+=" ) + distance * 2;
  45. // Animate
  46. element.animate( animation, speed, options.easing );
  47. // Shakes
  48. for ( ; i < times; i++ ) {
  49. element
  50. .animate( animation1, speed, options.easing )
  51. .animate( animation2, speed, options.easing );
  52. }
  53. element
  54. .animate( animation1, speed, options.easing )
  55. .animate( animation, speed / 2, options.easing )
  56. .queue( done );
  57. $.effects.unshift( element, queuelen, anims + 1 );
  58. } );
  59. } ) );