Skip to content

Commit 28ee5c6

Browse files
committed
Added the new .offset() method, directly imported from Dimensions (heavily worked over by both Brandon and John).
Original source: http://brandonaaron.net/jquery/issues/dimensions/new_offset/jquery.offset.js
1 parent d2f82ab commit 28ee5c6

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ BASE_FILES = ${SRC_DIR}/core.js\
1212
${SRC_DIR}/selector.js\
1313
${SRC_DIR}/event.js\
1414
${SRC_DIR}/ajax.js\
15-
${SRC_DIR}/fx.js
15+
${SRC_DIR}/fx.js\
16+
${SRC_DIR}/offset.js
1617

1718
PLUGINS = ${PLUG_DIR}/button/*\
1819
${PLUG_DIR}/center/*\

build.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@
4848
<fileset dir="${SRC_DIR}" includes="core.js" />
4949
<fileset dir="${SRC_DIR}" includes="selector.js" />
5050
<fileset dir="${SRC_DIR}" includes="event.js" />
51-
<fileset dir="${SRC_DIR}" includes="fx.js" />
5251
<fileset dir="${SRC_DIR}" includes="ajax.js" />
52+
<fileset dir="${SRC_DIR}" includes="fx.js" />
53+
<fileset dir="${SRC_DIR}" includes="offset.js" />
5354
<fileset dir="${PLUGIN_DIR}" includes="${PLUGINS}" />
5455
<fileset dir="${SRC_DIR}" includes="outro.js" />
5556
</concat>

src/offset.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
jQuery.fn.offset = function() {
2+
var left = 0, top = 0, elem = this[0], results;
3+
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;
10+
11+
// Use getBoundingClientRect if available
12+
if ( elem.getBoundingClientRect ) {
13+
box = elem.getBoundingClientRect();
14+
15+
// Add the document scroll offsets
16+
add(
17+
box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
18+
box.top + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)
19+
);
20+
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
24+
if ( msie ) {
25+
var border = jQuery("html").css("borderWidth");
26+
border = (border == "medium" || jQuery.boxModel && parseInt(version) >= 7) && 2 || border;
27+
add( -border, -border );
28+
}
29+
30+
// Otherwise loop through the offsetParents and parentNodes
31+
} else {
32+
33+
// Initial element offsets
34+
add( elem.offsetLeft, elem.offsetTop );
35+
36+
// Get parent offsets
37+
while ( offsetParent ) {
38+
// Add offsetParent offsets
39+
add( offsetParent.offsetLeft, offsetParent.offsetTop );
40+
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 );
45+
46+
// Safari <= 2 doubles body offsets with an absolutely positioned element or parent
47+
if ( safari2 && !absolute && jQuery.css(offsetParent, "position") == "absolute" )
48+
absolute = true;
49+
50+
// Get next offsetParent
51+
offsetParent = offsetParent.offsetParent;
52+
}
53+
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 );
60+
61+
// Mozilla does not add the border for a parent that has overflow != visible
62+
if ( mozilla && jQuery.css(parent, "overflow") != "visible" )
63+
border( parent );
64+
65+
// Get next parent
66+
parent = parent.parentNode;
67+
}
68+
69+
// Safari doubles body offsets with an absolutely positioned element or parent
70+
if ( safari && absolute )
71+
add( -doc.body.offsetLeft, -doc.body.offsetTop );
72+
}
73+
74+
// Return an object with top and left properties
75+
results = { top: top, left: left };
76+
}
77+
78+
return results;
79+
80+
function border(elem) {
81+
add( jQuery.css(elem, "borderLeftWidth"), jQuery.css(elem, "borderTopWidth") );
82+
}
83+
84+
function add(l, t) {
85+
left += parseInt(l) || 0;
86+
top += parseInt(t) || 0;
87+
}
88+
};

0 commit comments

Comments
 (0)