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 parent = elem.parentNode,
9 offsetParent = elem.offsetParent,
10 doc = elem.ownerDocument,
11 safari2 = safari && parseInt(version) < 522,
12 position = jQuery.css(elem, "position"),
13 absolute = position == "absolute",
14 fixed = position == "fixed";
16 // Use getBoundingClientRect if available
17 if ( elem.getBoundingClientRect ) {
18 var box = elem.getBoundingClientRect();
20 // Add the document scroll offsets
22 box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
23 box.top + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)
26 // IE adds the HTML element's border, by default it is medium which is 2px
27 // IE 6 and IE 7 quirks mode the border width is overwritable by the following css html { border: 0; }
28 // IE 7 standards mode, the border is always 2px
30 var border = jQuery("html").css("borderWidth");
31 border = (border == "medium" || jQuery.boxModel && parseInt(version) >= 7) && 2 || border;
32 add( -border, -border );
35 // Otherwise loop through the offsetParents and parentNodes
38 // Initial element offsets
39 add( elem.offsetLeft, elem.offsetTop );
42 while ( offsetParent ) {
43 // Add offsetParent offsets
44 add( offsetParent.offsetLeft, offsetParent.offsetTop );
46 // Mozilla and Safari > 2 does not include the border on offset parents
47 // However Mozilla adds the border for table cells
48 if ( mozilla && /^t[d|h]$/i.test(parent.tagName) || !safari2 )
49 border( offsetParent );
51 // Get offsetParent's position
52 position = jQuery.css(offsetParent, "position");
54 // Safari <= 2 doubles body offsets with an absolutely positioned element or parent
55 if ( safari2 && !absolute && position == "absolute" )
58 // Opera adds border for fixed, relative and absolute parent elements
59 if (opera && /^fixed|relative|absolute$/i.test(position))
61 -parseInt(jQuery.css(elem, "borderLeftWidth")),
62 -parseInt(jQuery.css(elem, "borderTopWidth"))
65 // Add the document scroll offsets if position is fixed
66 if ( !fixed && position == "fixed" )
69 // Get next offsetParent
70 offsetParent = offsetParent.offsetParent;
73 // Get parent scroll offsets
74 while ( parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
75 // Work around opera inline/table scrollLeft/Top bug
76 if ( !/^inline|table-row.*$/i.test(jQuery.css(parent, "display")) )
77 // Subtract parent scroll offsets
78 add( -parent.scrollLeft, -parent.scrollTop );
80 // Mozilla does not add the border for a parent that has overflow != visible
81 if ( mozilla && jQuery.css(parent, "overflow") != "visible" )
85 parent = parent.parentNode;
88 // Safari <= 2 doubles body offsets with an absolute or fixed positioned element or parent
89 if ( safari2 && (absolute || fixed) )
90 add( -doc.body.offsetLeft, -doc.body.offsetTop );
92 // Add the document scroll offsets if position is fixed
95 Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
96 Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)
100 // Return an object with top and left properties
101 results = { top: top, left: left };
106 function border(elem) {
107 add( jQuery.css(elem, "borderLeftWidth"), jQuery.css(elem, "borderTopWidth") );
111 left += parseInt(l) || 0;
112 top += parseInt(t) || 0;