effect-shake.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*!
  2. * jQuery UI Effects Shake 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/shake-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.shake = function( o, done ) {
  24. var el = $( this ),
  25. props = [ "position", "top", "bottom", "left", "right", "height", "width" ],
  26. mode = $.effects.setMode( el, o.mode || "effect" ),
  27. direction = o.direction || "left",
  28. distance = o.distance || 20,
  29. times = o.times || 3,
  30. anims = times * 2 + 1,
  31. speed = Math.round( o.duration / anims ),
  32. ref = (direction === "up" || direction === "down") ? "top" : "left",
  33. positiveMotion = (direction === "up" || direction === "left"),
  34. animation = {},
  35. animation1 = {},
  36. animation2 = {},
  37. i,
  38. // we will need to re-assemble the queue to stack our animations in place
  39. queue = el.queue(),
  40. queuelen = queue.length;
  41. $.effects.save( el, props );
  42. el.show();
  43. $.effects.createWrapper( el );
  44. // Animation
  45. animation[ ref ] = ( positiveMotion ? "-=" : "+=" ) + distance;
  46. animation1[ ref ] = ( positiveMotion ? "+=" : "-=" ) + distance * 2;
  47. animation2[ ref ] = ( positiveMotion ? "-=" : "+=" ) + distance * 2;
  48. // Animate
  49. el.animate( animation, speed, o.easing );
  50. // Shakes
  51. for ( i = 1; i < times; i++ ) {
  52. el.animate( animation1, speed, o.easing ).animate( animation2, speed, o.easing );
  53. }
  54. el
  55. .animate( animation1, speed, o.easing )
  56. .animate( animation, speed / 2, o.easing )
  57. .queue(function() {
  58. if ( mode === "hide" ) {
  59. el.hide();
  60. }
  61. $.effects.restore( el, props );
  62. $.effects.removeWrapper( el );
  63. done();
  64. });
  65. // inject all the animations we just queued to be first in line (after "inprogress")
  66. if ( queuelen > 1) {
  67. queue.splice.apply( queue,
  68. [ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) );
  69. }
  70. el.dequeue();
  71. };
  72. }));