jquery.ui.effect-shake.js 1.9KB

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