1
1
if ( "getBoundingClientRect" in document . documentElement ) {
2
- jQuery . fn . offset = function ( ) {
2
+ jQuery . fn . offset = function ( options ) {
3
3
var elem = this [ 0 ] ;
4
4
if ( ! elem || ! elem . ownerDocument ) { return null ; }
5
+ if ( options ) {
6
+ return this . each ( function ( ) {
7
+ jQuery . offset . setOffset ( this , options ) ;
8
+ } ) ;
9
+ }
5
10
if ( elem === elem . ownerDocument . body ) {
6
11
return jQuery . offset . bodyOffset ( elem ) ;
7
12
}
@@ -13,9 +18,14 @@ if ( "getBoundingClientRect" in document.documentElement ) {
13
18
return { top : top , left : left } ;
14
19
} ;
15
20
} else {
16
- jQuery . fn . offset = function ( ) {
21
+ jQuery . fn . offset = function ( options ) {
17
22
var elem = this [ 0 ] ;
18
23
if ( ! elem || ! elem . ownerDocument ) { return null ; }
24
+ if ( options ) {
25
+ return this . each ( function ( ) {
26
+ jQuery . offset . setOffset ( this , options ) ;
27
+ } ) ;
28
+ }
19
29
if ( elem === elem . ownerDocument . body ) {
20
30
return jQuery . offset . bodyOffset ( elem ) ;
21
31
}
@@ -25,18 +35,18 @@ if ( "getBoundingClientRect" in document.documentElement ) {
25
35
var offsetParent = elem . offsetParent , prevOffsetParent = elem ,
26
36
doc = elem . ownerDocument , computedStyle , docElem = doc . documentElement ,
27
37
body = doc . body , defaultView = doc . defaultView ,
28
- prevComputedStyle = defaultView . getComputedStyle ( elem , null ) ,
38
+ prevComputedStyle = defaultView . getComputedStyle ( elem , null ) ,
29
39
top = elem . offsetTop , left = elem . offsetLeft ;
30
40
31
41
while ( ( elem = elem . parentNode ) && elem !== body && elem !== docElem ) {
32
42
if ( jQuery . offset . supportsFixedPosition && prevComputedStyle . position === "fixed" ) { break ; }
33
43
34
44
computedStyle = defaultView . getComputedStyle ( elem , null ) ;
35
- top -= elem . scrollTop ;
45
+ top -= elem . scrollTop ;
36
46
left -= elem . scrollLeft ;
37
47
38
48
if ( elem === offsetParent ) {
39
- top += elem . offsetTop ;
49
+ top += elem . offsetTop ;
40
50
left += elem . offsetLeft ;
41
51
42
52
if ( jQuery . offset . doesNotAddBorder && ! ( jQuery . offset . doesAddBorderForTableAndCells && / ^ t ( a b l e | d | h ) $ / i. test ( elem . nodeName ) ) ) {
@@ -100,7 +110,7 @@ jQuery.offset = {
100
110
jQuery . offset . initialize = function ( ) { } ;
101
111
} ,
102
112
103
- bodyOffset : function ( body ) {
113
+ bodyOffset : function ( body ) {
104
114
var top = body . offsetTop , left = body . offsetLeft ;
105
115
106
116
jQuery . offset . initialize ( ) ;
@@ -111,6 +121,27 @@ jQuery.offset = {
111
121
}
112
122
113
123
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 ( / s t a t i c / . 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
+ }
114
145
}
115
146
} ;
116
147
0 commit comments