jquery.dimensions.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  3. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  4. *
  5. * $LastChangedDate: 2007-03-04 20:15:11 -0600 (Sun, 04 Mar 2007) $
  6. * $Rev: 1485 $
  7. */
  8. jQuery.fn._height = jQuery.fn.height;
  9. jQuery.fn._width = jQuery.fn.width;
  10. /**
  11. * If used on document, returns the document's height (innerHeight)
  12. * If used on window, returns the viewport's (window) height
  13. * See core docs on height() to see what happens when used on an element.
  14. *
  15. * @example $("#testdiv").height()
  16. * @result 200
  17. *
  18. * @example $(document).height()
  19. * @result 800
  20. *
  21. * @example $(window).height()
  22. * @result 400
  23. *
  24. * @name height
  25. * @type Object
  26. * @cat Plugins/Dimensions
  27. */
  28. jQuery.fn.height = function() {
  29. if ( this[0] == window )
  30. return self.innerHeight ||
  31. jQuery.boxModel && document.documentElement.clientHeight ||
  32. document.body.clientHeight;
  33. if ( this[0] == document )
  34. return Math.max( document.body.scrollHeight, document.body.offsetHeight );
  35. return this._height(arguments[0]);
  36. };
  37. /**
  38. * If used on document, returns the document's width (innerWidth)
  39. * If used on window, returns the viewport's (window) width
  40. * See core docs on height() to see what happens when used on an element.
  41. *
  42. * @example $("#testdiv").width()
  43. * @result 200
  44. *
  45. * @example $(document).width()
  46. * @result 800
  47. *
  48. * @example $(window).width()
  49. * @result 400
  50. *
  51. * @name width
  52. * @type Object
  53. * @cat Plugins/Dimensions
  54. */
  55. jQuery.fn.width = function() {
  56. if ( this[0] == window )
  57. return self.innerWidth ||
  58. jQuery.boxModel && document.documentElement.clientWidth ||
  59. document.body.clientWidth;
  60. if ( this[0] == document )
  61. return Math.max( document.body.scrollWidth, document.body.offsetWidth );
  62. return this._width(arguments[0]);
  63. };
  64. /**
  65. * Returns the inner height value (without border) for the first matched element.
  66. * If used on document, returns the document's height (innerHeight)
  67. * If used on window, returns the viewport's (window) height
  68. *
  69. * @example $("#testdiv").innerHeight()
  70. * @result 800
  71. *
  72. * @name innerHeight
  73. * @type Number
  74. * @cat Plugins/Dimensions
  75. */
  76. jQuery.fn.innerHeight = function() {
  77. return this[0] == window || this[0] == document ?
  78. this.height() :
  79. this.css('display') != 'none' ?
  80. this[0].offsetHeight - (parseInt(this.css("borderTopWidth")) || 0) - (parseInt(this.css("borderBottomWidth")) || 0) :
  81. this.height() + (parseInt(this.css("paddingTop")) || 0) + (parseInt(this.css("paddingBottom")) || 0);
  82. };
  83. /**
  84. * Returns the inner width value (without border) for the first matched element.
  85. * If used on document, returns the document's Width (innerWidth)
  86. * If used on window, returns the viewport's (window) width
  87. *
  88. * @example $("#testdiv").innerWidth()
  89. * @result 1000
  90. *
  91. * @name innerWidth
  92. * @type Number
  93. * @cat Plugins/Dimensions
  94. */
  95. jQuery.fn.innerWidth = function() {
  96. return this[0] == window || this[0] == document ?
  97. this.width() :
  98. this.css('display') != 'none' ?
  99. this[0].offsetWidth - (parseInt(this.css("borderLeftWidth")) || 0) - (parseInt(this.css("borderRightWidth")) || 0) :
  100. this.height() + (parseInt(this.css("paddingLeft")) || 0) + (parseInt(this.css("paddingRight")) || 0);
  101. };
  102. /**
  103. * Returns the outer height value (including border) for the first matched element.
  104. * Cannot be used on document or window.
  105. *
  106. * @example $("#testdiv").outerHeight()
  107. * @result 1000
  108. *
  109. * @name outerHeight
  110. * @type Number
  111. * @cat Plugins/Dimensions
  112. */
  113. jQuery.fn.outerHeight = function() {
  114. return this[0] == window || this[0] == document ?
  115. this.height() :
  116. this.css('display') != 'none' ?
  117. this[0].offsetHeight :
  118. this.height() + (parseInt(this.css("borderTopWidth")) || 0) + (parseInt(this.css("borderBottomWidth")) || 0)
  119. + (parseInt(this.css("paddingTop")) || 0) + (parseInt(this.css("paddingBottom")) || 0);
  120. };
  121. /**
  122. * Returns the outer width value (including border) for the first matched element.
  123. * Cannot be used on document or window.
  124. *
  125. * @example $("#testdiv").outerWidth()
  126. * @result 1000
  127. *
  128. * @name outerWidth
  129. * @type Number
  130. * @cat Plugins/Dimensions
  131. */
  132. jQuery.fn.outerWidth = function() {
  133. return this[0] == window || this[0] == document ?
  134. this.width() :
  135. this.css('display') != 'none' ?
  136. this[0].offsetWidth :
  137. this.height() + (parseInt(this.css("borderLeftWidth")) || 0) + (parseInt(this.css("borderRightWidth")) || 0)
  138. + (parseInt(this.css("paddingLeft")) || 0) + (parseInt(this.css("paddingRight")) || 0);
  139. };
  140. /**
  141. * Returns how many pixels the user has scrolled to the right (scrollLeft).
  142. * Works on containers with overflow: auto and window/document.
  143. *
  144. * @example $("#testdiv").scrollLeft()
  145. * @result 100
  146. *
  147. * @name scrollLeft
  148. * @type Number
  149. * @cat Plugins/Dimensions
  150. */
  151. jQuery.fn.scrollLeft = function() {
  152. if ( this[0] == window || this[0] == document )
  153. return self.pageXOffset ||
  154. jQuery.boxModel && document.documentElement.scrollLeft ||
  155. document.body.scrollLeft;
  156. return this[0].scrollLeft;
  157. };
  158. /**
  159. * Returns how many pixels the user has scrolled to the bottom (scrollTop).
  160. * Works on containers with overflow: auto and window/document.
  161. *
  162. * @example $("#testdiv").scrollTop()
  163. * @result 100
  164. *
  165. * @name scrollTop
  166. * @type Number
  167. * @cat Plugins/Dimensions
  168. */
  169. jQuery.fn.scrollTop = function() {
  170. if ( this[0] == window || this[0] == document )
  171. return self.pageYOffset ||
  172. jQuery.boxModel && document.documentElement.scrollTop ||
  173. document.body.scrollTop;
  174. return this[0].scrollTop;
  175. };
  176. /**
  177. * Returns the location of the element in pixels from the top left corner of the viewport.
  178. *
  179. * For accurate readings make sure to use pixel values for margins, borders and padding.
  180. *
  181. * @example $("#testdiv").offset()
  182. * @result { top: 100, left: 100, scrollTop: 10, scrollLeft: 10 }
  183. *
  184. * @example $("#testdiv").offset({ scroll: false })
  185. * @result { top: 90, left: 90 }
  186. *
  187. * @example var offset = {}
  188. * $("#testdiv").offset({ scroll: false }, offset)
  189. * @result offset = { top: 90, left: 90 }
  190. *
  191. * @name offset
  192. * @param Object options A hash of options describing what should be included in the final calculations of the offset.
  193. * The options include:
  194. * margin: Should the margin of the element be included in the calculations? True by default.
  195. * If set to false the margin of the element is subtracted from the total offset.
  196. * border: Should the border of the element be included in the calculations? True by default.
  197. * If set to false the border of the element is subtracted from the total offset.
  198. * padding: Should the padding of the element be included in the calculations? False by default.
  199. * If set to true the padding of the element is added to the total offset.
  200. * scroll: Should the scroll offsets of the parent elements be included in the calculations?
  201. * True by default. When true, it adds the total scroll offsets of all parents to the
  202. * total offset and also adds two properties to the returned object, scrollTop and
  203. * scrollLeft. If set to false the scroll offsets of parent elements are ignored.
  204. * If scroll offsets are not needed, set to false to get a performance boost.
  205. * @param Object returnObject An object to store the return value in, so as not to break the chain. If passed in the
  206. * chain will not be broken and the result will be assigned to this object.
  207. * @type Object
  208. * @cat Plugins/Dimensions
  209. * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
  210. */
  211. jQuery.fn.offset = function(options, returnObject) {
  212. var x = 0, y = 0, elem = this[0], parent = this[0], op, sl = 0, st = 0, options = jQuery.extend({ margin: true, border: true, padding: false, scroll: true }, options || {});
  213. do {
  214. x += parent.offsetLeft || 0;
  215. y += parent.offsetTop || 0;
  216. // Mozilla and IE do not add the border
  217. if (jQuery.browser.mozilla || jQuery.browser.msie) {
  218. // get borders
  219. var bt = parseInt(jQuery.css(parent, 'borderTopWidth')) || 0;
  220. var bl = parseInt(jQuery.css(parent, 'borderLeftWidth')) || 0;
  221. // add borders to offset
  222. x += bl;
  223. y += bt;
  224. // Mozilla removes the border if the parent has overflow property other than visible
  225. if (jQuery.browser.mozilla && parent != elem && jQuery.css(parent, 'overflow') != 'visible') {
  226. x += bl;
  227. y += bt;
  228. }
  229. }
  230. if (options.scroll) {
  231. // Need to get scroll offsets in-between offsetParents
  232. op = parent.offsetParent;
  233. do {
  234. sl += parent.scrollLeft || 0;
  235. st += parent.scrollTop || 0;
  236. parent = parent.parentNode;
  237. // Mozilla removes the border if the parent has overflow property other than visible
  238. if (jQuery.browser.mozilla && parent != elem && parent != op && jQuery.css(parent, 'overflow') != 'visible') {
  239. y += parseInt(jQuery.css(parent, 'borderTopWidth')) || 0;
  240. x += parseInt(jQuery.css(parent, 'borderLeftWidth')) || 0;
  241. }
  242. } while (parent != op);
  243. } else
  244. parent = parent.offsetParent;
  245. if (parent && (parent.tagName.toLowerCase() == 'body' || parent.tagName.toLowerCase() == 'html')) {
  246. // Safari doesn't add the body margin for elments positioned with static or relative
  247. if ((jQuery.browser.safari || (jQuery.browser.msie && jQuery.boxModel)) && jQuery.css(parent, 'position') != 'absolute') {
  248. x += parseInt(jQuery.css(op, 'marginLeft')) || 0;
  249. y += parseInt(jQuery.css(op, 'marginTop')) || 0;
  250. }
  251. break; // Exit the loop
  252. }
  253. } while (parent);
  254. if ( !options.margin) {
  255. x -= parseInt(jQuery.css(elem, 'marginLeft')) || 0;
  256. y -= parseInt(jQuery.css(elem, 'marginTop')) || 0;
  257. }
  258. // Safari and Opera do not add the border for the element
  259. if ( options.border && (jQuery.browser.safari || jQuery.browser.opera) ) {
  260. x += parseInt(jQuery.css(elem, 'borderLeftWidth')) || 0;
  261. y += parseInt(jQuery.css(elem, 'borderTopWidth')) || 0;
  262. } else if ( !options.border && !(jQuery.browser.safari || jQuery.browser.opera) ) {
  263. x -= parseInt(jQuery.css(elem, 'borderLeftWidth')) || 0;
  264. y -= parseInt(jQuery.css(elem, 'borderTopWidth')) || 0;
  265. }
  266. if ( options.padding ) {
  267. x += parseInt(jQuery.css(elem, 'paddingLeft')) || 0;
  268. y += parseInt(jQuery.css(elem, 'paddingTop')) || 0;
  269. }
  270. // Opera thinks offset is scroll offset for display: inline elements
  271. if (options.scroll && jQuery.browser.opera && jQuery.css(elem, 'display') == 'inline') {
  272. sl -= elem.scrollLeft || 0;
  273. st -= elem.scrollTop || 0;
  274. }
  275. var returnValue = options.scroll ? { top: y - st, left: x - sl, scrollTop: st, scrollLeft: sl }
  276. : { top: y, left: x };
  277. if (returnObject) { jQuery.extend(returnObject, returnValue); return this; }
  278. else { return returnValue; }
  279. };