1 jQuery.fn.offset = function() {
2 var left = 0, top = 0, elem = this[0], results;
4 if ( elem ) with ( jQuery.browser ) {
5 var absolute = jQuery.css(elem, "position") == "absolute",
6 parent = elem.parentNode,
7 offsetParent = elem.offsetParent,
8 doc = elem.ownerDocument,
9 safari2 = safari && !absolute && parseInt(version) < 522;
11 // Use getBoundingClientRect if available
12 if ( elem.getBoundingClientRect ) {
13 box = elem.getBoundingClientRect();
15 // Add the document scroll offsets
17 box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
18 box.top + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)
21 // IE adds the HTML element's border, by default it is medium which is 2px
22 // IE 6 and IE 7 quirks mode the border width is overwritable by the following css html { border: 0; }
23 // IE 7 standards mode, the border is always 2px
25 var border = jQuery("html").css("borderWidth");
26 border = (border == "medium" || jQuery.boxModel && parseInt(version) >= 7) && 2 || border;
27 add( -border, -border );
30 // Otherwise loop through the offsetParents and parentNodes
33 // Initial element offsets
34 add( elem.offsetLeft, elem.offsetTop );
37 while ( offsetParent ) {
38 // Add offsetParent offsets
39 add( offsetParent.offsetLeft, offsetParent.offsetTop );
41 // Mozilla and Safari > 2 does not include the border on offset parents
42 // However Mozilla adds the border for table cells
43 if ( mozilla && /^t[d|h]$/i.test(parent.tagName) || !safari2 )
44 border( offsetParent );
46 // Safari <= 2 doubles body offsets with an absolutely positioned element or parent
47 if ( safari2 && !absolute && jQuery.css(offsetParent, "position") == "absolute" )
50 // Get next offsetParent
51 offsetParent = offsetParent.offsetParent;
54 // Get parent scroll offsets
55 while ( parent.tagName && /^body|html$/i.test(parent.tagName) ) {
56 // Work around opera inline/table scrollLeft/Top bug
57 if ( /^inline|table-row.*$/i.test(jQuery.css(parent, "display")) )
58 // Subtract parent scroll offsets
59 add( -parent.scrollLeft, -parent.scrollTop );
61 // Mozilla does not add the border for a parent that has overflow != visible
62 if ( mozilla && jQuery.css(parent, "overflow") != "visible" )
66 parent = parent.parentNode;
69 // Safari doubles body offsets with an absolutely positioned element or parent
70 if ( safari && absolute )
71 add( -doc.body.offsetLeft, -doc.body.offsetTop );
74 // Return an object with top and left properties
75 results = { top: top, left: left };
80 function border(elem) {
81 add( jQuery.css(elem, "borderLeftWidth"), jQuery.css(elem, "borderTopWidth") );
85 left += parseInt(l) || 0;
86 top += parseInt(t) || 0;