2 // Originally By Brandon Aaron, part of the Dimension Plugin
3 // http://jquery.com/plugins/project/dimensions
4 jQuery.fn.offset = function() {
5 var left = 0, top = 0, elem = this[0], results;
7 if ( elem ) with ( jQuery.browser ) {
8 var absolute = jQuery.css(elem, "position") == "absolute",
9 parent = elem.parentNode,
10 offsetParent = elem.offsetParent,
11 doc = elem.ownerDocument,
12 safari2 = safari && parseInt(version) < 522;
14 // Use getBoundingClientRect if available
15 if ( elem.getBoundingClientRect ) {
16 box = elem.getBoundingClientRect();
18 // Add the document scroll offsets
20 box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
21 box.top + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)
24 // IE adds the HTML element's border, by default it is medium which is 2px
25 // IE 6 and IE 7 quirks mode the border width is overwritable by the following css html { border: 0; }
26 // IE 7 standards mode, the border is always 2px
28 var border = jQuery("html").css("borderWidth");
29 border = (border == "medium" || jQuery.boxModel && parseInt(version) >= 7) && 2 || border;
30 add( -border, -border );
33 // Otherwise loop through the offsetParents and parentNodes
36 // Initial element offsets
37 add( elem.offsetLeft, elem.offsetTop );
40 while ( offsetParent ) {
41 // Add offsetParent offsets
42 add( offsetParent.offsetLeft, offsetParent.offsetTop );
44 // Mozilla and Safari > 2 does not include the border on offset parents
45 // However Mozilla adds the border for table cells
46 if ( mozilla && /^t[d|h]$/i.test(parent.tagName) || !safari2 )
47 border( offsetParent );
49 // Safari <= 2 doubles body offsets with an absolutely positioned element or parent
50 if ( safari2 && !absolute && jQuery.css(offsetParent, "position") == "absolute" )
53 // Get next offsetParent
54 offsetParent = offsetParent.offsetParent;
57 // Get parent scroll offsets
58 while ( parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
59 // Work around opera inline/table scrollLeft/Top bug
60 if ( !/^inline|table-row.*$/i.test(jQuery.css(parent, "display")) )
61 // Subtract parent scroll offsets
62 add( -parent.scrollLeft, -parent.scrollTop );
64 // Mozilla does not add the border for a parent that has overflow != visible
65 if ( mozilla && jQuery.css(parent, "overflow") != "visible" )
69 parent = parent.parentNode;
72 // Safari doubles body offsets with an absolutely positioned element or parent
73 if ( safari2 && absolute )
74 add( -doc.body.offsetLeft, -doc.body.offsetTop );
77 // Return an object with top and left properties
78 results = { top: top, left: left };
83 function border(elem) {
84 add( jQuery.css(elem, "borderLeftWidth"), jQuery.css(elem, "borderTopWidth") );
88 left += parseInt(l) || 0;
89 top += parseInt(t) || 0;