Rolled back Joern's changes. They've been moved to another branch, and will be merged...
[jquery.git] / src / dimensions / dimensions.js
index ce87862..1a8e87a 100644 (file)
@@ -9,29 +9,29 @@
  * If used on window, returns the viewport's (window) height\r
  *\r
  * @example $("#testdiv").height()\r
- * @result "200px"
- *
- * @example $(document).height();
- * @result 800
- *
- * @example $(window).height();
+ * @result "200px"\r
+ *\r
+ * @example $(document).height();\r
+ * @result 800\r
+ *\r
+ * @example $(window).height();\r
  * @result 400\r
  * \r
  * @name height\r
  * @type Object\r
  * @cat Dimensions\r
  */\r
-$.fn.height = function() {\r
-       if ( this.get(0) == window )
-               return self.innerHeight ||
+jQuery.fn.height = function() {\r
+       if ( this.get(0) == window )\r
+               return self.innerHeight ||\r
                        jQuery.boxModel && document.documentElement.clientHeight ||\r
                        document.body.clientHeight;\r
        \r
        if ( this.get(0) == document )\r
                return Math.max( document.body.scrollHeight, document.body.offsetHeight );\r
        \r
-       return this.css("height");\r
-};
+       return this.css("height", arguments[0]);\r
+};\r
 \r
 /**\r
  * Returns the css width value for the first matched element.\r
@@ -39,29 +39,29 @@ $.fn.height = function() {
  * If used on window, returns the viewport's (window) width\r
  *\r
  * @example $("#testdiv").width()\r
- * @result "200px"
- *
- * @example $(document).width();
- * @result 800
- *
- * @example $(window).width();
+ * @result "200px"\r
+ *\r
+ * @example $(document).width();\r
+ * @result 800\r
+ *\r
+ * @example $(window).width();\r
  * @result 400\r
  * \r
  * @name width\r
  * @type Object\r
  * @cat Dimensions\r
  */\r
-$.fn.width = function() {\r
-       if ( this.get(0) == window )
-               return self.innerWidth ||
+jQuery.fn.width = function() {\r
+       if ( this.get(0) == window )\r
+               return self.innerWidth ||\r
                        jQuery.boxModel && document.documentElement.clientWidth ||\r
                        document.body.clientWidth;\r
        \r
        if ( this.get(0) == document )\r
                return Math.max( document.body.scrollWidth, document.body.offsetWidth );\r
        \r
-       return this.css("width");\r
-};
+       return this.css("width", arguments[0]);\r
+};\r
 \r
 /**\r
  * Returns the inner height value (without border) for the first matched element.\r
@@ -75,11 +75,11 @@ $.fn.width = function() {
  * @type Number\r
  * @cat Dimensions\r
  */\r
-$.fn.innerHeight = function() {\r
-       return this.get(0) == window || this.get(0) == document ?
-               this.height() :
+jQuery.fn.innerHeight = function() {\r
+       return this.get(0) == window || this.get(0) == document ?\r
+               this.height() :\r
                this.get(0).offsetHeight - parseInt(this.css("borderTop") || 0) - parseInt(this.css("borderBottom") || 0);\r
-};
+};\r
 \r
 /**\r
  * Returns the inner width value (without border) for the first matched element.\r
@@ -93,11 +93,11 @@ $.fn.innerHeight = function() {
  * @type Number\r
  * @cat Dimensions\r
  */\r
-$.fn.innerWidth = function() {\r
-       return this.get(0) == window || this.get(0) == document ?
-               this.width() :
+jQuery.fn.innerWidth = function() {\r
+       return this.get(0) == window || this.get(0) == document ?\r
+               this.width() :\r
                this.get(0).offsetWidth - parseInt(this.css("borderLeft") || 0) - parseInt(this.css("borderRight") || 0);\r
-};
+};\r
 \r
 /**\r
  * Returns the outer height value (including border) for the first matched element.\r
@@ -110,11 +110,11 @@ $.fn.innerWidth = function() {
  * @type Number\r
  * @cat Dimensions\r
  */\r
-$.fn.outerHeight = function() {\r
-       return this.get(0) == window || this.get(0) == document ?
-               this.height() :
+jQuery.fn.outerHeight = function() {\r
+       return this.get(0) == window || this.get(0) == document ?\r
+               this.height() :\r
                this.get(0).offsetHeight;       \r
-};
+};\r
 \r
 /**\r
  * Returns the outer width value (including border) for the first matched element.\r
@@ -127,11 +127,11 @@ $.fn.outerHeight = function() {
  * @type Number\r
  * @cat Dimensions\r
  */\r
-$.fn.outerWidth = function() {\r
-       return this.get(0) == window || this.get(0) == document ?
-               this.width() :
+jQuery.fn.outerWidth = function() {\r
+       return this.get(0) == window || this.get(0) == document ?\r
+               this.width() :\r
                this.get(0).offsetWidth;        \r
-};
+};\r
 \r
 /**\r
  * Returns how many pixels the user has scrolled to the right (scrollLeft).\r
@@ -144,14 +144,14 @@ $.fn.outerWidth = function() {
  * @type Number\r
  * @cat Dimensions\r
  */\r
-$.fn.scrollLeft = function() {\r
-       if ( this.get(0) == window || this.get(0) == document )
-               return self.pageXOffset ||
-                       jQuery.boxModel && document.documentElement.scrollLeft ||
-                       document.body.scrollLeft;
+jQuery.fn.scrollLeft = function() {\r
+       if ( this.get(0) == window || this.get(0) == document )\r
+               return self.pageXOffset ||\r
+                       jQuery.boxModel && document.documentElement.scrollLeft ||\r
+                       document.body.scrollLeft;\r
        \r
        return this.get(0).scrollLeft;\r
-};
+};\r
 \r
 /**\r
  * Returns how many pixels the user has scrolled to the bottom (scrollTop).\r
@@ -164,20 +164,15 @@ $.fn.scrollLeft = function() {
  * @type Number\r
  * @cat Dimensions\r
  */\r
-$.fn.scrollTop = function() {\r
-       if ( this.get(0) == window || this.get(0) == document )
-               return self.pageYOffset ||
-                       jQuery.boxModel && document.documentElement.scrollTop ||
-                       document.body.scrollTop;
+jQuery.fn.scrollTop = function() {\r
+       if ( this.get(0) == window || this.get(0) == document )\r
+               return self.pageYOffset ||\r
+                       jQuery.boxModel && document.documentElement.scrollTop ||\r
+                       document.body.scrollTop;\r
 \r
        return this.get(0).scrollTop;\r
 };\r
 \r
-/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)\r
- * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) \r
- * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.\r
- */\r
-\r
 /**\r
  * This returns an object with top, left, width, height, borderLeft,\r
  * borderTop, marginLeft, marginTop, scrollLeft, scrollTop, \r
@@ -253,57 +248,62 @@ $.fn.scrollTop = function() {
  * @cat Dimensions\r
  * @author Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)\r
  */\r
-$.fn.offset = function(refElem) {\r
-       if (!this[0]) throw '$.fn.offset requires an element.';\r
-       \r
-       refElem = (refElem) ? $(refElem)[0] : null;\r
-       var x = 0, y = 0, elm = this[0], parent = this[0], pos = null, borders = [0,0], isElm = true, sl = 0, st = 0;\r
+jQuery.fn.offset = function(refElem) {\r
+       if (!this[0]) throw 'jQuery.fn.offset requires an element.';\r
+\r
+       refElem = (refElem) ? jQuery(refElem)[0] : null;\r
+       var x = 0, y = 0, elem = this[0], parent = this[0], sl = 0, st = 0;\r
        do {\r
                if (parent.tagName == 'BODY' || parent.tagName == 'HTML') {\r
                        // Safari and IE don't add margin for static and relative\r
-                       if (($.browser.safari || $.browser.msie) && pos != 'absolute') {\r
-                               x += parseInt($.css(parent, 'marginLeft')) || 0;\r
-                               y += parseInt($.css(parent, 'marginTop'))  || 0;\r
+                       if ((jQuery.browser.safari || jQuery.browser.msie) && jQuery.css(parent, 'position') != 'absolute') {\r
+                               x += parseInt(jQuery.css(parent, 'marginLeft')) || 0;\r
+                               y += parseInt(jQuery.css(parent, 'marginTop'))  || 0;\r
                        }\r
                        break;\r
                }\r
-               \r
-               pos    = $.css(parent, 'position');\r
-               border = [parseInt($.css(parent, 'borderLeftWidth')) || 0,\r
-                                                       parseInt($.css(parent, 'borderTopWidth'))  || 0];\r
-               sl = parent.scrollLeft;\r
-               st = parent.scrollTop;\r
-               \r
-               x += (parent.offsetLeft || 0) + border[0] - sl;\r
-               y += (parent.offsetTop  || 0) + border[1] - st;\r
-               \r
-               // Safari and Opera include the border already for parents with position = absolute|relative\r
-               if (($.browser.safari || $.browser.opera) && !isElm && (pos == 'absolute' || pos == 'relative')) {\r
-                       x -= border[0];\r
-                       y -= border[1];\r
+\r
+               x += parent.offsetLeft || 0;\r
+               y += parent.offsetTop  || 0;\r
+\r
+               // Mozilla and IE do not add the border\r
+               if (jQuery.browser.mozilla || jQuery.browser.msie) {\r
+                       x += parseInt(jQuery.css(parent, 'borderLeftWidth')) || 0;\r
+                       y += parseInt(jQuery.css(parent, 'borderTopWidth'))  || 0;\r
                }\r
-               \r
-               parent = parent.offsetParent;\r
-               isElm  = false;\r
-       } while(parent);\r
-       \r
-       if (refElem) {\r
-               var offset = $(refElem).offset();\r
+\r
+               // Need to get scroll offsets in-between offsetParents\r
+               var op = parent.offsetParent;\r
+               do {\r
+                       sl += parent.scrollLeft || 0;\r
+                       st += parent.scrollTop  || 0;\r
+                       parent = parent.parentNode;\r
+               } while (parent != op);\r
+       } while (parent);\r
+\r
+       if (refElem) { // Get the relative offset\r
+               var offset = jQuery(refElem).offset();\r
                x  = x  - offset.left;\r
                y  = y  - offset.top;\r
                sl = sl - offset.scrollLeft;\r
                st = st - offset.scrollTop;\r
        }\r
-       \r
+\r
+       // Safari and Opera do not add the border for the element\r
+       if (jQuery.browser.safari || jQuery.browser.opera) {\r
+               x += parseInt(jQuery.css(elem, 'borderLeftWidth')) || 0;\r
+               y += parseInt(jQuery.css(elem, 'borderTopWidth'))  || 0;\r
+       }\r
+\r
        return {\r
-               top:  y,\r
-               left: x,\r
-               width:  elm.offsetWidth,\r
-               height: elm.offsetHeight,\r
-               borderTop:  parseInt($.css(elm, 'borderTopWidth'))  || 0,\r
-               borderLeft: parseInt($.css(elm, 'borderLeftWidth')) || 0,\r
-               marginTop:  parseInt($.css(elm, 'marginTopWidth'))  || 0,\r
-               marginLeft: parseInt($.css(elm, 'marginLeftWidth')) || 0,\r
+               top:  y - st,\r
+               left: x - sl,\r
+               width:  elem.offsetWidth,\r
+               height: elem.offsetHeight,\r
+               borderTop:  parseInt(jQuery.css(elem, 'borderTopWidth'))  || 0,\r
+               borderLeft: parseInt(jQuery.css(elem, 'borderLeftWidth')) || 0,\r
+               marginTop:  parseInt(jQuery.css(elem, 'marginTopWidth'))  || 0,\r
+               marginLeft: parseInt(jQuery.css(elem, 'marginLeftWidth')) || 0,\r
                scrollTop:  st,\r
                scrollLeft: sl,\r
                pageYOffset: window.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop  || 0,\r