11if ( "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 ( a b l e | 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 ( / 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+ }
114145 }
115146} ;
116147
0 commit comments