effects.shake.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * jQuery UI Effects Shake 1.7.2
  3. *
  4. * Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
  5. * Dual licensed under the MIT (MIT-LICENSE.txt)
  6. * and GPL (GPL-LICENSE.txt) licenses.
  7. *
  8. * http://docs.jquery.com/UI/Effects/Shake
  9. *
  10. * Depends:
  11. * effects.core.js
  12. */
  13. (function($) {
  14. $.effects.shake = function(o) {
  15. return this.queue(function() {
  16. // Create element
  17. var el = $(this), props = ['position','top','left'];
  18. // Set options
  19. var mode = $.effects.setMode(el, o.options.mode || 'effect'); // Set Mode
  20. var direction = o.options.direction || 'left'; // Default direction
  21. var distance = o.options.distance || 20; // Default distance
  22. var times = o.options.times || 3; // Default # of times
  23. var speed = o.duration || o.options.duration || 140; // Default speed per shake
  24. // Adjust
  25. $.effects.save(el, props); el.show(); // Save & Show
  26. $.effects.createWrapper(el); // Create Wrapper
  27. var ref = (direction == 'up' || direction == 'down') ? 'top' : 'left';
  28. var motion = (direction == 'up' || direction == 'left') ? 'pos' : 'neg';
  29. // Animation
  30. var animation = {}, animation1 = {}, animation2 = {};
  31. animation[ref] = (motion == 'pos' ? '-=' : '+=') + distance;
  32. animation1[ref] = (motion == 'pos' ? '+=' : '-=') + distance * 2;
  33. animation2[ref] = (motion == 'pos' ? '-=' : '+=') + distance * 2;
  34. // Animate
  35. el.animate(animation, speed, o.options.easing);
  36. for (var i = 1; i < times; i++) { // Shakes
  37. el.animate(animation1, speed, o.options.easing).animate(animation2, speed, o.options.easing);
  38. };
  39. el.animate(animation1, speed, o.options.easing).
  40. animate(animation, speed / 2, o.options.easing, function(){ // Last shake
  41. $.effects.restore(el, props); $.effects.removeWrapper(el); // Restore
  42. if(o.callback) o.callback.apply(this, arguments); // Callback
  43. });
  44. el.queue('fx', function() { el.dequeue(); });
  45. el.dequeue();
  46. });
  47. };
  48. })(jQuery);