UNPKG

5.48 kBJavaScriptView Raw
1define([
2 "./core",
3 "./var/strundefined",
4 "./core/access",
5 "./css/var/rnumnonpx",
6 "./css/curCSS",
7 "./css/addGetHookIf",
8 "./css/support",
9
10 "./core/init",
11 "./css",
12 "./selector" // contains
13], function( jQuery, strundefined, access, rnumnonpx, curCSS, addGetHookIf, support ) {
14
15var docElem = window.document.documentElement;
16
17/**
18 * Gets a window from an element
19 */
20function getWindow( elem ) {
21 return jQuery.isWindow( elem ) ? elem : elem.nodeType === 9 && elem.defaultView;
22}
23
24jQuery.offset = {
25 setOffset: function( elem, options, i ) {
26 var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition,
27 position = jQuery.css( elem, "position" ),
28 curElem = jQuery( elem ),
29 props = {};
30
31 // Set position first, in-case top/left are set even on static elem
32 if ( position === "static" ) {
33 elem.style.position = "relative";
34 }
35
36 curOffset = curElem.offset();
37 curCSSTop = jQuery.css( elem, "top" );
38 curCSSLeft = jQuery.css( elem, "left" );
39 calculatePosition = ( position === "absolute" || position === "fixed" ) &&
40 ( curCSSTop + curCSSLeft ).indexOf("auto") > -1;
41
42 // Need to be able to calculate position if either top or left is auto and position is either absolute or fixed
43 if ( calculatePosition ) {
44 curPosition = curElem.position();
45 curTop = curPosition.top;
46 curLeft = curPosition.left;
47
48 } else {
49 curTop = parseFloat( curCSSTop ) || 0;
50 curLeft = parseFloat( curCSSLeft ) || 0;
51 }
52
53 if ( jQuery.isFunction( options ) ) {
54 options = options.call( elem, i, curOffset );
55 }
56
57 if ( options.top != null ) {
58 props.top = ( options.top - curOffset.top ) + curTop;
59 }
60 if ( options.left != null ) {
61 props.left = ( options.left - curOffset.left ) + curLeft;
62 }
63
64 if ( "using" in options ) {
65 options.using.call( elem, props );
66
67 } else {
68 curElem.css( props );
69 }
70 }
71};
72
73jQuery.fn.extend({
74 offset: function( options ) {
75 if ( arguments.length ) {
76 return options === undefined ?
77 this :
78 this.each(function( i ) {
79 jQuery.offset.setOffset( this, options, i );
80 });
81 }
82
83 var docElem, win,
84 elem = this[ 0 ],
85 box = { top: 0, left: 0 },
86 doc = elem && elem.ownerDocument;
87
88 if ( !doc ) {
89 return;
90 }
91
92 docElem = doc.documentElement;
93
94 // Make sure it's not a disconnected DOM node
95 if ( !jQuery.contains( docElem, elem ) ) {
96 return box;
97 }
98
99 // If we don't have gBCR, just use 0,0 rather than error
100 // BlackBerry 5, iOS 3 (original iPhone)
101 if ( typeof elem.getBoundingClientRect !== strundefined ) {
102 box = elem.getBoundingClientRect();
103 }
104 win = getWindow( doc );
105 return {
106 top: box.top + win.pageYOffset - docElem.clientTop,
107 left: box.left + win.pageXOffset - docElem.clientLeft
108 };
109 },
110
111 position: function() {
112 if ( !this[ 0 ] ) {
113 return;
114 }
115
116 var offsetParent, offset,
117 elem = this[ 0 ],
118 parentOffset = { top: 0, left: 0 };
119
120 // Fixed elements are offset from window (parentOffset = {top:0, left: 0}, because it is its only offset parent
121 if ( jQuery.css( elem, "position" ) === "fixed" ) {
122 // We assume that getBoundingClientRect is available when computed position is fixed
123 offset = elem.getBoundingClientRect();
124
125 } else {
126 // Get *real* offsetParent
127 offsetParent = this.offsetParent();
128
129 // Get correct offsets
130 offset = this.offset();
131 if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
132 parentOffset = offsetParent.offset();
133 }
134
135 // Add offsetParent borders
136 parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
137 parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
138 }
139
140 // Subtract parent offsets and element margins
141 return {
142 top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
143 left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true )
144 };
145 },
146
147 offsetParent: function() {
148 return this.map(function() {
149 var offsetParent = this.offsetParent || docElem;
150
151 while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" ) && jQuery.css( offsetParent, "position" ) === "static" ) ) {
152 offsetParent = offsetParent.offsetParent;
153 }
154
155 return offsetParent || docElem;
156 });
157 }
158});
159
160// Create scrollLeft and scrollTop methods
161jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
162 var top = "pageYOffset" === prop;
163
164 jQuery.fn[ method ] = function( val ) {
165 return access( this, function( elem, method, val ) {
166 var win = getWindow( elem );
167
168 if ( val === undefined ) {
169 return win ? win[ prop ] : elem[ method ];
170 }
171
172 if ( win ) {
173 win.scrollTo(
174 !top ? val : window.pageXOffset,
175 top ? val : window.pageYOffset
176 );
177
178 } else {
179 elem[ method ] = val;
180 }
181 }, method, val, arguments.length, null );
182 };
183});
184
185// Add the top/left cssHooks using jQuery.fn.position
186// Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
187// getComputedStyle returns percent when specified for top/left/bottom/right
188// rather than make the css module depend on the offset module, we just check for it here
189jQuery.each( [ "top", "left" ], function( i, prop ) {
190 jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
191 function( elem, computed ) {
192 if ( computed ) {
193 computed = curCSS( elem, prop );
194 // if curCSS returns percentage, fallback to offset
195 return rnumnonpx.test( computed ) ?
196 jQuery( elem ).position()[ prop ] + "px" :
197 computed;
198 }
199 }
200 );
201});
202
203return jQuery;
204});