effects.scale.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * jQuery UI Effects Scale 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/Scale
  9. *
  10. * Depends:
  11. * effects.core.js
  12. */
  13. (function($) {
  14. $.effects.puff = function(o) {
  15. return this.queue(function() {
  16. // Create element
  17. var el = $(this);
  18. // Set options
  19. var options = $.extend(true, {}, o.options);
  20. var mode = $.effects.setMode(el, o.options.mode || 'hide'); // Set Mode
  21. var percent = parseInt(o.options.percent,10) || 150; // Set default puff percent
  22. options.fade = true; // It's not a puff if it doesn't fade! :)
  23. var original = {height: el.height(), width: el.width()}; // Save original
  24. // Adjust
  25. var factor = percent / 100;
  26. el.from = (mode == 'hide') ? original : {height: original.height * factor, width: original.width * factor};
  27. // Animation
  28. options.from = el.from;
  29. options.percent = (mode == 'hide') ? percent : 100;
  30. options.mode = mode;
  31. // Animate
  32. el.effect('scale', options, o.duration, o.callback);
  33. el.dequeue();
  34. });
  35. };
  36. $.effects.scale = function(o) {
  37. return this.queue(function() {
  38. // Create element
  39. var el = $(this);
  40. // Set options
  41. var options = $.extend(true, {}, o.options);
  42. var mode = $.effects.setMode(el, o.options.mode || 'effect'); // Set Mode
  43. var percent = parseInt(o.options.percent,10) || (parseInt(o.options.percent,10) == 0 ? 0 : (mode == 'hide' ? 0 : 100)); // Set default scaling percent
  44. var direction = o.options.direction || 'both'; // Set default axis
  45. var origin = o.options.origin; // The origin of the scaling
  46. if (mode != 'effect') { // Set default origin and restore for show/hide
  47. options.origin = origin || ['middle','center'];
  48. options.restore = true;
  49. }
  50. var original = {height: el.height(), width: el.width()}; // Save original
  51. el.from = o.options.from || (mode == 'show' ? {height: 0, width: 0} : original); // Default from state
  52. // Adjust
  53. var factor = { // Set scaling factor
  54. y: direction != 'horizontal' ? (percent / 100) : 1,
  55. x: direction != 'vertical' ? (percent / 100) : 1
  56. };
  57. el.to = {height: original.height * factor.y, width: original.width * factor.x}; // Set to state
  58. if (o.options.fade) { // Fade option to support puff
  59. if (mode == 'show') {el.from.opacity = 0; el.to.opacity = 1;};
  60. if (mode == 'hide') {el.from.opacity = 1; el.to.opacity = 0;};
  61. };
  62. // Animation
  63. options.from = el.from; options.to = el.to; options.mode = mode;
  64. // Animate
  65. el.effect('size', options, o.duration, o.callback);
  66. el.dequeue();
  67. });
  68. };
  69. $.effects.size = function(o) {
  70. return this.queue(function() {
  71. // Create element
  72. var el = $(this), props = ['position','top','left','width','height','overflow','opacity'];
  73. var props1 = ['position','top','left','overflow','opacity']; // Always restore
  74. var props2 = ['width','height','overflow']; // Copy for children
  75. var cProps = ['fontSize'];
  76. var vProps = ['borderTopWidth', 'borderBottomWidth', 'paddingTop', 'paddingBottom'];
  77. var hProps = ['borderLeftWidth', 'borderRightWidth', 'paddingLeft', 'paddingRight'];
  78. // Set options
  79. var mode = $.effects.setMode(el, o.options.mode || 'effect'); // Set Mode
  80. var restore = o.options.restore || false; // Default restore
  81. var scale = o.options.scale || 'both'; // Default scale mode
  82. var origin = o.options.origin; // The origin of the sizing
  83. var original = {height: el.height(), width: el.width()}; // Save original
  84. el.from = o.options.from || original; // Default from state
  85. el.to = o.options.to || original; // Default to state
  86. // Adjust
  87. if (origin) { // Calculate baseline shifts
  88. var baseline = $.effects.getBaseline(origin, original);
  89. el.from.top = (original.height - el.from.height) * baseline.y;
  90. el.from.left = (original.width - el.from.width) * baseline.x;
  91. el.to.top = (original.height - el.to.height) * baseline.y;
  92. el.to.left = (original.width - el.to.width) * baseline.x;
  93. };
  94. var factor = { // Set scaling factor
  95. from: {y: el.from.height / original.height, x: el.from.width / original.width},
  96. to: {y: el.to.height / original.height, x: el.to.width / original.width}
  97. };
  98. if (scale == 'box' || scale == 'both') { // Scale the css box
  99. if (factor.from.y != factor.to.y) { // Vertical props scaling
  100. props = props.concat(vProps);
  101. el.from = $.effects.setTransition(el, vProps, factor.from.y, el.from);
  102. el.to = $.effects.setTransition(el, vProps, factor.to.y, el.to);
  103. };
  104. if (factor.from.x != factor.to.x) { // Horizontal props scaling
  105. props = props.concat(hProps);
  106. el.from = $.effects.setTransition(el, hProps, factor.from.x, el.from);
  107. el.to = $.effects.setTransition(el, hProps, factor.to.x, el.to);
  108. };
  109. };
  110. if (scale == 'content' || scale == 'both') { // Scale the content
  111. if (factor.from.y != factor.to.y) { // Vertical props scaling
  112. props = props.concat(cProps);
  113. el.from = $.effects.setTransition(el, cProps, factor.from.y, el.from);
  114. el.to = $.effects.setTransition(el, cProps, factor.to.y, el.to);
  115. };
  116. };
  117. $.effects.save(el, restore ? props : props1); el.show(); // Save & Show
  118. $.effects.createWrapper(el); // Create Wrapper
  119. el.css('overflow','hidden').css(el.from); // Shift
  120. // Animate
  121. if (scale == 'content' || scale == 'both') { // Scale the children
  122. vProps = vProps.concat(['marginTop','marginBottom']).concat(cProps); // Add margins/font-size
  123. hProps = hProps.concat(['marginLeft','marginRight']); // Add margins
  124. props2 = props.concat(vProps).concat(hProps); // Concat
  125. el.find("*[width]").each(function(){
  126. child = $(this);
  127. if (restore) $.effects.save(child, props2);
  128. var c_original = {height: child.height(), width: child.width()}; // Save original
  129. child.from = {height: c_original.height * factor.from.y, width: c_original.width * factor.from.x};
  130. child.to = {height: c_original.height * factor.to.y, width: c_original.width * factor.to.x};
  131. if (factor.from.y != factor.to.y) { // Vertical props scaling
  132. child.from = $.effects.setTransition(child, vProps, factor.from.y, child.from);
  133. child.to = $.effects.setTransition(child, vProps, factor.to.y, child.to);
  134. };
  135. if (factor.from.x != factor.to.x) { // Horizontal props scaling
  136. child.from = $.effects.setTransition(child, hProps, factor.from.x, child.from);
  137. child.to = $.effects.setTransition(child, hProps, factor.to.x, child.to);
  138. };
  139. child.css(child.from); // Shift children
  140. child.animate(child.to, o.duration, o.options.easing, function(){
  141. if (restore) $.effects.restore(child, props2); // Restore children
  142. }); // Animate children
  143. });
  144. };
  145. // Animate
  146. el.animate(el.to, { queue: false, duration: o.duration, easing: o.options.easing, complete: function() {
  147. if(mode == 'hide') el.hide(); // Hide
  148. $.effects.restore(el, restore ? props : props1); $.effects.removeWrapper(el); // Restore
  149. if(o.callback) o.callback.apply(this, arguments); // Callback
  150. el.dequeue();
  151. }});
  152. });
  153. };
  154. })(jQuery);