jquery.box.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ;(function($) {
  2. if ($.fn.box) {
  3. return;
  4. }
  5. // sum the layout box style definitions.
  6. function plus() {
  7. var value = 0, fnName;
  8. for (var i = 0; i < arguments.length; i++) {
  9. fnName = $.trim(arguments[i]);
  10. if (this[fnName]) {
  11. value += this[fnName]();
  12. }
  13. }
  14. return value;
  15. };
  16. $.fn.box = function() {
  17. var $this = this.eq(0);
  18. var map = {
  19. "ih": function() {
  20. delete this["ih"];
  21. return $this.height();
  22. },
  23. "iw": function() {
  24. delete this["iw"];
  25. return $this.width();
  26. },
  27. "oh": function() {
  28. delete this["oh"];
  29. return plus.call(this, "ih", "mt", "mb", "bt", "bb", "pt", "pb");
  30. },
  31. "ow": function() {
  32. delete this["ow"];
  33. return plus.call(this, "iw", "ml", "mr", "bl", "br", "pl", "pr");
  34. },
  35. "ml": function() {
  36. delete this["ml"];
  37. return (parseInt($this.css("margin-left")) || 0);
  38. },
  39. "mr": function() {
  40. delete this["mr"];
  41. return (parseInt($this.css("margin-right")) || 0);
  42. },
  43. "mt": function() {
  44. delete this["mt"];
  45. return (parseInt($this.css("margin-top")) || 0);
  46. },
  47. "mb": function() {
  48. delete this["mb"];
  49. return (parseInt($this.css("margin-bottom")) || 0);
  50. },
  51. "bl": function() {
  52. delete this["bl"];
  53. return (parseInt($this.css("border-left-width")) || 0);
  54. },
  55. "br": function() {
  56. delete this["br"];
  57. return (parseInt($this.css("border-right-width")) || 0);
  58. },
  59. "bt": function() {
  60. delete this["bt"];
  61. return (parseInt($this.css("border-top-width")) || 0);
  62. },
  63. "bb": function() {
  64. delete this["bb"];
  65. return (parseInt($this.css("border-bottom-width")) || 0);
  66. },
  67. "pl": function() {
  68. delete this["pl"];
  69. return (parseInt($this.css("padding-left")) || 0);
  70. },
  71. "pr": function() {
  72. delete this["pr"];
  73. return (parseInt($this.css("padding-right")) || 0);
  74. },
  75. "pt": function() {
  76. delete this["pt"];
  77. return (parseInt($this.css("padding-top")) || 0);
  78. },
  79. "pb": function() {
  80. delete this["pb"];
  81. return (parseInt($this.css("padding-bottom")) || 0);
  82. },
  83. "mlr": function() {
  84. delete this["mlr"];
  85. return plus.call(this, "ml", "mr");
  86. },
  87. "mtb": function() {
  88. delete this["mtb"];
  89. return plus.call(this, "mt", "mb");
  90. },
  91. "blr": function() {
  92. delete this["blr"];
  93. return plus.call(this, "bl", "br");
  94. },
  95. "btb": function() {
  96. delete this["btb"];
  97. return plus.call(this, "bt", "bb");
  98. },
  99. "plr": function() {
  100. delete this["plr"];
  101. return plus.call(this, "pl", "pr");
  102. },
  103. "ptb": function() {
  104. delete this["ptb"];
  105. return plus.call(this, "pt", "pb");
  106. },
  107. "l": function() {
  108. delete this["l"];
  109. return plus.call(this, "ml", "bl", "pl");
  110. },
  111. "r": function() {
  112. delete this["r"];
  113. return plus.call(this, "mr", "br", "pr");
  114. },
  115. "t": function() {
  116. delete this["t"];
  117. return plus.call(this, "mt", "bt", "pt");
  118. },
  119. "b": function() {
  120. delete this["b"];
  121. return plus.call(this, "mb", "bb", "pb");
  122. },
  123. "lr": function() {
  124. delete this["lr"];
  125. return plus.call(this, "ml", "mr", "bl", "br", "pl", "pr");
  126. },
  127. "tb": function() {
  128. delete this["tb"];
  129. return plus.call(this, "mt", "mb", "bt", "bb", "pt", "pb");
  130. }
  131. };
  132. return plus.apply(map, arguments);
  133. };
  134. $.fn.boxWidth = function(width) {
  135. if (typeof width == "undefined") {
  136. return this.is(":visible") ? this.box("ow") : 0;
  137. }
  138. else {
  139. return this.each(function() {
  140. var $this = $(this);
  141. $this.width(width - $this.box("lr"))
  142. });
  143. }
  144. };
  145. $.fn.boxHeight = function(height) {
  146. if (typeof height == "undefined") {
  147. return this.is(":visible") ? this.box("oh") : 0;
  148. }
  149. else {
  150. return this.each(function() {
  151. var $this = $(this);
  152. $this.height(height - $this.box("tb"))
  153. });
  154. }
  155. };
  156. })(jQuery);