dimensions.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * This plugin overrides jQuery's height() and width() functions and
  3. * adds more handy stuff for cross-browser compatibility.
  4. */
  5. /**
  6. * Returns the css height value for the first matched element.
  7. * If used on document, returns the document's height (innerHeight)
  8. * If used on window, returns the viewport's (window) height
  9. *
  10. * @example $("#testdiv").height()
  11. * @result "200px"
  12. *
  13. * @example $(document).height();
  14. * @result 800
  15. *
  16. * @example $(window).height();
  17. * @result 400
  18. *
  19. * @name height
  20. * @type Object
  21. * @cat Plugins/Dimensions
  22. */
  23. jQuery.fn.height = function() {
  24. if ( this.get(0) == window )
  25. return self.innerHeight ||
  26. jQuery.boxModel && document.documentElement.clientHeight ||
  27. document.body.clientHeight;
  28. if ( this.get(0) == document )
  29. return Math.max( document.body.scrollHeight, document.body.offsetHeight );
  30. return this.css("height", arguments[0]);
  31. };
  32. /**
  33. * Returns the css width value for the first matched element.
  34. * If used on document, returns the document's width (innerWidth)
  35. * If used on window, returns the viewport's (window) width
  36. *
  37. * @example $("#testdiv").width()
  38. * @result "200px"
  39. *
  40. * @example $(document).width();
  41. * @result 800
  42. *
  43. * @example $(window).width();
  44. * @result 400
  45. *
  46. * @name width
  47. * @type Object
  48. * @cat Plugins/Dimensions
  49. */
  50. jQuery.fn.width = function() {
  51. if ( this.get(0) == window )
  52. return self.innerWidth ||
  53. jQuery.boxModel && document.documentElement.clientWidth ||
  54. document.body.clientWidth;
  55. if ( this.get(0) == document )
  56. return Math.max( document.body.scrollWidth, document.body.offsetWidth );
  57. return this.css("width", arguments[0]);
  58. };
  59. /**
  60. * Returns the inner height value (without border) for the first matched element.
  61. * If used on document, returns the document's height (innerHeight)
  62. * If used on window, returns the viewport's (window) height
  63. *
  64. * @example $("#testdiv").innerHeight()
  65. * @result 800
  66. *
  67. * @name innerHeight
  68. * @type Number
  69. * @cat Plugins/Dimensions
  70. */
  71. jQuery.fn.innerHeight = function() {
  72. return this.get(0) == window || this.get(0) == document ?
  73. this.height() :
  74. this.get(0).offsetHeight - parseInt(this.css("borderTop") || 0) - parseInt(this.css("borderBottom") || 0);
  75. };
  76. /**
  77. * Returns the inner width value (without border) for the first matched element.
  78. * If used on document, returns the document's Width (innerWidth)
  79. * If used on window, returns the viewport's (window) width
  80. *
  81. * @example $("#testdiv").innerWidth()
  82. * @result 1000
  83. *
  84. * @name innerWidth
  85. * @type Number
  86. * @cat Plugins/Dimensions
  87. */
  88. jQuery.fn.innerWidth = function() {
  89. return this.get(0) == window || this.get(0) == document ?
  90. this.width() :
  91. this.get(0).offsetWidth - parseInt(this.css("borderLeft") || 0) - parseInt(this.css("borderRight") || 0);
  92. };
  93. /**
  94. * Returns the outer height value (including border) for the first matched element.
  95. * Cannot be used on document or window.
  96. *
  97. * @example $("#testdiv").outerHeight()
  98. * @result 1000
  99. *
  100. * @name outerHeight
  101. * @type Number
  102. * @cat Plugins/Dimensions
  103. */
  104. jQuery.fn.outerHeight = function() {
  105. return this.get(0) == window || this.get(0) == document ?
  106. this.height() :
  107. this.get(0).offsetHeight;
  108. };
  109. /**
  110. * Returns the outer width value (including border) for the first matched element.
  111. * Cannot be used on document or window.
  112. *
  113. * @example $("#testdiv").outerWidth()
  114. * @result 1000
  115. *
  116. * @name outerWidth
  117. * @type Number
  118. * @cat Plugins/Dimensions
  119. */
  120. jQuery.fn.outerWidth = function() {
  121. return this.get(0) == window || this.get(0) == document ?
  122. this.width() :
  123. this.get(0).offsetWidth;
  124. };
  125. /**
  126. * Returns how many pixels the user has scrolled to the right (scrollLeft).
  127. * Works on containers with overflow: auto and window/document.
  128. *
  129. * @example $("#testdiv").scrollLeft()
  130. * @result 100
  131. *
  132. * @name scrollLeft
  133. * @type Number
  134. * @cat Plugins/Dimensions
  135. */
  136. jQuery.fn.scrollLeft = function() {
  137. if ( this.get(0) == window || this.get(0) == document )
  138. return self.pageXOffset ||
  139. jQuery.boxModel && document.documentElement.scrollLeft ||
  140. document.body.scrollLeft;
  141. return this.get(0).scrollLeft;
  142. };
  143. /**
  144. * Returns how many pixels the user has scrolled to the bottom (scrollTop).
  145. * Works on containers with overflow: auto and window/document.
  146. *
  147. * @example $("#testdiv").scrollTop()
  148. * @result 100
  149. *
  150. * @name scrollTop
  151. * @type Number
  152. * @cat Plugins/Dimensions
  153. */
  154. jQuery.fn.scrollTop = function() {
  155. if ( this.get(0) == window || this.get(0) == document )
  156. return self.pageYOffset ||
  157. jQuery.boxModel && document.documentElement.scrollTop ||
  158. document.body.scrollTop;
  159. return this.get(0).scrollTop;
  160. };
  161. /**
  162. * This returns an object with top, left, width, height, borderLeft,
  163. * borderTop, marginLeft, marginTop, scrollLeft, scrollTop,
  164. * pageXOffset, pageYOffset.
  165. *
  166. * The top and left values include the scroll offsets but the
  167. * scrollLeft and scrollTop properties of the returned object
  168. * are the combined scroll offets of the parent elements
  169. * (not including the window scroll offsets). This is not the
  170. * same as the element's scrollTop and scrollLeft.
  171. *
  172. * For accurate readings make sure to use pixel values.
  173. *
  174. * @name offset
  175. * @type Object
  176. * @cat Plugins/Dimensions
  177. * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
  178. */
  179. /**
  180. * This returns an object with top, left, width, height, borderLeft,
  181. * borderTop, marginLeft, marginTop, scrollLeft, scrollTop,
  182. * pageXOffset, pageYOffset.
  183. *
  184. * The top and left values include the scroll offsets but the
  185. * scrollLeft and scrollTop properties of the returned object
  186. * are the combined scroll offets of the parent elements
  187. * (not including the window scroll offsets). This is not the
  188. * same as the element's scrollTop and scrollLeft.
  189. *
  190. * For accurate readings make sure to use pixel values.
  191. *
  192. * @name offset
  193. * @type Object
  194. * @param String refElement This is an expression. The offset returned will be relative to the first matched element.
  195. * @cat Plugins/Dimensions
  196. * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
  197. */
  198. /**
  199. * This returns an object with top, left, width, height, borderLeft,
  200. * borderTop, marginLeft, marginTop, scrollLeft, scrollTop,
  201. * pageXOffset, pageYOffset.
  202. *
  203. * The top and left values include the scroll offsets but the
  204. * scrollLeft and scrollTop properties of the returned object
  205. * are the combined scroll offets of the parent elements
  206. * (not including the window scroll offsets). This is not the
  207. * same as the element's scrollTop and scrollLeft.
  208. *
  209. * For accurate readings make sure to use pixel values.
  210. *
  211. * @name offset
  212. * @type Object
  213. * @param jQuery refElement The offset returned will be relative to the first matched element.
  214. * @cat Plugins/Dimensions
  215. * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
  216. */
  217. /**
  218. * This returns an object with top, left, width, height, borderLeft,
  219. * borderTop, marginLeft, marginTop, scrollLeft, scrollTop,
  220. * pageXOffset, pageYOffset.
  221. *
  222. * The top and left values include the scroll offsets but the
  223. * scrollLeft and scrollTop properties of the returned object
  224. * are the combined scroll offets of the parent elements
  225. * (not including the window scroll offsets). This is not the
  226. * same as the element's scrollTop and scrollLeft.
  227. *
  228. * For accurate readings make sure to use pixel values.
  229. *
  230. * @name offset
  231. * @type Object
  232. * @param HTMLElement refElement The offset returned will be relative to this element.
  233. * @cat Plugins/Dimensions
  234. * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
  235. */
  236. jQuery.fn.offset = function(refElem) {
  237. if (!this[0]) throw 'jQuery.fn.offset requires an element.';
  238. refElem = (refElem) ? jQuery(refElem)[0] : null;
  239. var x = 0, y = 0, elem = this[0], parent = this[0], sl = 0, st = 0;
  240. do {
  241. if (parent.tagName == 'BODY' || parent.tagName == 'HTML') {
  242. // Safari and IE don't add margin for static and relative
  243. if ((jQuery.browser.safari || jQuery.browser.msie) && jQuery.css(parent, 'position') != 'absolute') {
  244. x += parseInt(jQuery.css(parent, 'marginLeft')) || 0;
  245. y += parseInt(jQuery.css(parent, 'marginTop')) || 0;
  246. }
  247. break;
  248. }
  249. x += parent.offsetLeft || 0;
  250. y += parent.offsetTop || 0;
  251. // Mozilla and IE do not add the border
  252. if (jQuery.browser.mozilla || jQuery.browser.msie) {
  253. x += parseInt(jQuery.css(parent, 'borderLeftWidth')) || 0;
  254. y += parseInt(jQuery.css(parent, 'borderTopWidth')) || 0;
  255. }
  256. // Mozilla removes the border if the parent has overflow hidden
  257. if (jQuery.browser.mozilla && jQuery.css(parent, 'overflow') == 'hidden') {
  258. x += parseInt(jQuery.css(parent, 'borderLeftWidth')) || 0;
  259. y += parseInt(jQuery.css(parent, 'borderTopWidth')) || 0;
  260. }
  261. // Need to get scroll offsets in-between offsetParents
  262. var op = parent.offsetParent;
  263. do {
  264. sl += parent.scrollLeft || 0;
  265. st += parent.scrollTop || 0;
  266. parent = parent.parentNode;
  267. } while (parent != op);
  268. } while (parent);
  269. if (refElem) { // Get the relative offset
  270. var offset = jQuery(refElem).offset();
  271. x = x - offset.left;
  272. y = y - offset.top;
  273. sl = sl - offset.scrollLeft;
  274. st = st - offset.scrollTop;
  275. }
  276. // Safari and Opera do not add the border for the element
  277. if (jQuery.browser.safari || jQuery.browser.opera) {
  278. x += parseInt(jQuery.css(elem, 'borderLeftWidth')) || 0;
  279. y += parseInt(jQuery.css(elem, 'borderTopWidth')) || 0;
  280. }
  281. return {
  282. top: y - st,
  283. left: x - sl,
  284. width: elem.offsetWidth,
  285. height: elem.offsetHeight,
  286. borderTop: parseInt(jQuery.css(elem, 'borderTopWidth')) || 0,
  287. borderLeft: parseInt(jQuery.css(elem, 'borderLeftWidth')) || 0,
  288. marginTop: parseInt(jQuery.css(elem, 'marginTopWidth')) || 0,
  289. marginLeft: parseInt(jQuery.css(elem, 'marginLeftWidth')) || 0,
  290. scrollTop: st,
  291. scrollLeft: sl,
  292. pageYOffset: window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0,
  293. pageXOffset: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0
  294. };
  295. };