effects.bounce.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * jQuery UI Effects Bounce 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/Bounce
  9. *
  10. * Depends:
  11. * effects.core.js
  12. */
  13. (function($) {
  14. $.effects.bounce = 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 || 'up'; // Default direction
  21. var distance = o.options.distance || 20; // Default distance
  22. var times = o.options.times || 5; // Default # of times
  23. var speed = o.duration || 250; // Default speed per bounce
  24. if (/show|hide/.test(mode)) props.push('opacity'); // Avoid touching opacity to prevent clearType and PNG issues in IE
  25. // Adjust
  26. $.effects.save(el, props); el.show(); // Save & Show
  27. $.effects.createWrapper(el); // Create Wrapper
  28. var ref = (direction == 'up' || direction == 'down') ? 'top' : 'left';
  29. var motion = (direction == 'up' || direction == 'left') ? 'pos' : 'neg';
  30. var distance = o.options.distance || (ref == 'top' ? el.outerHeight({margin:true}) / 3 : el.outerWidth({margin:true}) / 3);
  31. if (mode == 'show') el.css('opacity', 0).css(ref, motion == 'pos' ? -distance : distance); // Shift
  32. if (mode == 'hide') distance = distance / (times * 2);
  33. if (mode != 'hide') times--;
  34. // Animate
  35. if (mode == 'show') { // Show Bounce
  36. var animation = {opacity: 1};
  37. animation[ref] = (motion == 'pos' ? '+=' : '-=') + distance;
  38. el.animate(animation, speed / 2, o.options.easing);
  39. distance = distance / 2;
  40. times--;
  41. };
  42. for (var i = 0; i < times; i++) { // Bounces
  43. var animation1 = {}, animation2 = {};
  44. animation1[ref] = (motion == 'pos' ? '-=' : '+=') + distance;
  45. animation2[ref] = (motion == 'pos' ? '+=' : '-=') + distance;
  46. el.animate(animation1, speed / 2, o.options.easing).animate(animation2, speed / 2, o.options.easing);
  47. distance = (mode == 'hide') ? distance * 2 : distance / 2;
  48. };
  49. if (mode == 'hide') { // Last Bounce
  50. var animation = {opacity: 0};
  51. animation[ref] = (motion == 'pos' ? '-=' : '+=') + distance;
  52. el.animate(animation, speed / 2, o.options.easing, function(){
  53. el.hide(); // Hide
  54. $.effects.restore(el, props); $.effects.removeWrapper(el); // Restore
  55. if(o.callback) o.callback.apply(this, arguments); // Callback
  56. });
  57. } else {
  58. var animation1 = {}, animation2 = {};
  59. animation1[ref] = (motion == 'pos' ? '-=' : '+=') + distance;
  60. animation2[ref] = (motion == 'pos' ? '+=' : '-=') + distance;
  61. el.animate(animation1, speed / 2, o.options.easing).animate(animation2, speed / 2, o.options.easing, function(){
  62. $.effects.restore(el, props); $.effects.removeWrapper(el); // Restore
  63. if(o.callback) o.callback.apply(this, arguments); // Callback
  64. });
  65. };
  66. el.queue('fx', function() { el.dequeue(); });
  67. el.dequeue();
  68. });
  69. };
  70. })(jQuery);