Skip to content

Commit daffb95

Browse files
committed
offset is now a setter
1 parent f55fb36 commit daffb95

File tree

4 files changed

+248
-83
lines changed

4 files changed

+248
-83
lines changed

src/offset.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
if ( "getBoundingClientRect" in document.documentElement ) {
2-
jQuery.fn.offset = function() {
2+
jQuery.fn.offset = function( options ) {
33
var elem = this[0];
44
if ( !elem || !elem.ownerDocument ) { return null; }
5+
if ( options ) {
6+
return this.each(function() {
7+
jQuery.offset.setOffset( this, options );
8+
});
9+
}
510
if ( elem === elem.ownerDocument.body ) {
611
return jQuery.offset.bodyOffset( elem );
712
}
@@ -13,9 +18,14 @@ if ( "getBoundingClientRect" in document.documentElement ) {
1318
return { top: top, left: left };
1419
};
1520
} else {
16-
jQuery.fn.offset = function() {
21+
jQuery.fn.offset = function( options ) {
1722
var elem = this[0];
1823
if ( !elem || !elem.ownerDocument ) { return null; }
24+
if ( options ) {
25+
return this.each(function() {
26+
jQuery.offset.setOffset( this, options );
27+
});
28+
}
1929
if ( elem === elem.ownerDocument.body ) {
2030
return jQuery.offset.bodyOffset( elem );
2131
}
@@ -25,18 +35,18 @@ if ( "getBoundingClientRect" in document.documentElement ) {
2535
var offsetParent = elem.offsetParent, prevOffsetParent = elem,
2636
doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
2737
body = doc.body, defaultView = doc.defaultView,
28-
prevComputedStyle = defaultView.getComputedStyle(elem, null),
38+
prevComputedStyle = defaultView.getComputedStyle( elem, null ),
2939
top = elem.offsetTop, left = elem.offsetLeft;
3040

3141
while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
3242
if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) { break; }
3343

3444
computedStyle = defaultView.getComputedStyle(elem, null);
35-
top -= elem.scrollTop;
45+
top -= elem.scrollTop;
3646
left -= elem.scrollLeft;
3747

3848
if ( elem === offsetParent ) {
39-
top += elem.offsetTop;
49+
top += elem.offsetTop;
4050
left += elem.offsetLeft;
4151

4252
if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.nodeName)) ) {
@@ -100,7 +110,7 @@ jQuery.offset = {
100110
jQuery.offset.initialize = function(){};
101111
},
102112

103-
bodyOffset: function(body) {
113+
bodyOffset: function( body ) {
104114
var top = body.offsetTop, left = body.offsetLeft;
105115

106116
jQuery.offset.initialize();
@@ -111,6 +121,27 @@ jQuery.offset = {
111121
}
112122

113123
return { top: top, left: left };
124+
},
125+
126+
setOffset: function( elem, options ) {
127+
// set position first, in-case top/left are set even on static elem
128+
if ( /static/.test( jQuery.curCSS( elem, 'position' ) ) ) {
129+
elem.style.position = 'relative';
130+
}
131+
var curElem = jQuery( elem ),
132+
curOffset = curElem.offset(),
133+
curTop = parseInt( jQuery.curCSS( elem, 'top', true ), 10 ) || 0,
134+
curLeft = parseInt( jQuery.curCSS( elem, 'left', true ), 10) || 0,
135+
props = {
136+
top: (options.top - curOffset.top) + curTop,
137+
left: (options.left - curOffset.left) + curLeft
138+
};
139+
140+
if ( 'using' in options ) {
141+
options.using.call( elem, props );
142+
} else {
143+
curElem.css( props );
144+
}
114145
}
115146
};
116147

test/data/offset/relative.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>relative</title>
77
<style type="text/css" media="screen">
88
body { margin: 1px; padding: 5px; }
9-
div.relative { position: relative; margin: 1px; border: 2px solid #000; padding: 5px; width: 100px; height: 100px; background: #fff; overflow: hidden; }
9+
div.relative { position: relative; top: 0; left: 0; margin: 1px; border: 2px solid #000; padding: 5px; width: 100px; height: 100px; background: #fff; overflow: hidden; }
1010
#relative-2 { top: 20px; left: 20px; }
1111
#marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; }
1212
</style>

test/data/offset/static.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>static</title>
77
<style type="text/css" media="screen">
88
body { margin: 1px; padding: 5px; }
9-
div.static { position: static; margin: 1px; border: 2px solid #000; padding: 5px; width: 100px; height: 100px; background: #fff; overflow: hidden; }
9+
div.static { position: static; top: 0; left: 0; margin: 1px; border: 2px solid #000; padding: 5px; width: 100px; height: 100px; background: #fff; overflow: hidden; }
1010
#static-2 { top: 20px; left: 20px; }
1111
#marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; }
1212
</style>

0 commit comments

Comments
 (0)