Projet

Général

Profil

Paste
Télécharger (5,72 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / flexslider-2.5.0 / bower_components / jquery / src / offset.js @ 0aee3c58

1
define([
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

    
15
// BuildExclude
16
curCSS = curCSS.curCSS;
17

    
18
var docElem = window.document.documentElement;
19

    
20
/**
21
 * Gets a window from an element
22
 */
23
function getWindow( elem ) {
24
        return jQuery.isWindow( elem ) ?
25
                elem :
26
                elem.nodeType === 9 ?
27
                        elem.defaultView || elem.parentWindow :
28
                        false;
29
}
30

    
31
jQuery.offset = {
32
        setOffset: function( elem, options, i ) {
33
                var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition,
34
                        position = jQuery.css( elem, "position" ),
35
                        curElem = jQuery( elem ),
36
                        props = {};
37

    
38
                // set position first, in-case top/left are set even on static elem
39
                if ( position === "static" ) {
40
                        elem.style.position = "relative";
41
                }
42

    
43
                curOffset = curElem.offset();
44
                curCSSTop = jQuery.css( elem, "top" );
45
                curCSSLeft = jQuery.css( elem, "left" );
46
                calculatePosition = ( position === "absolute" || position === "fixed" ) &&
47
                        jQuery.inArray("auto", [ curCSSTop, curCSSLeft ] ) > -1;
48

    
49
                // need to be able to calculate position if either top or left is auto and position is either absolute or fixed
50
                if ( calculatePosition ) {
51
                        curPosition = curElem.position();
52
                        curTop = curPosition.top;
53
                        curLeft = curPosition.left;
54
                } else {
55
                        curTop = parseFloat( curCSSTop ) || 0;
56
                        curLeft = parseFloat( curCSSLeft ) || 0;
57
                }
58

    
59
                if ( jQuery.isFunction( options ) ) {
60
                        options = options.call( elem, i, curOffset );
61
                }
62

    
63
                if ( options.top != null ) {
64
                        props.top = ( options.top - curOffset.top ) + curTop;
65
                }
66
                if ( options.left != null ) {
67
                        props.left = ( options.left - curOffset.left ) + curLeft;
68
                }
69

    
70
                if ( "using" in options ) {
71
                        options.using.call( elem, props );
72
                } else {
73
                        curElem.css( props );
74
                }
75
        }
76
};
77

    
78
jQuery.fn.extend({
79
        offset: function( options ) {
80
                if ( arguments.length ) {
81
                        return options === undefined ?
82
                                this :
83
                                this.each(function( i ) {
84
                                        jQuery.offset.setOffset( this, options, i );
85
                                });
86
                }
87

    
88
                var docElem, win,
89
                        box = { top: 0, left: 0 },
90
                        elem = this[ 0 ],
91
                        doc = elem && elem.ownerDocument;
92

    
93
                if ( !doc ) {
94
                        return;
95
                }
96

    
97
                docElem = doc.documentElement;
98

    
99
                // Make sure it's not a disconnected DOM node
100
                if ( !jQuery.contains( docElem, elem ) ) {
101
                        return box;
102
                }
103

    
104
                // If we don't have gBCR, just use 0,0 rather than error
105
                // BlackBerry 5, iOS 3 (original iPhone)
106
                if ( typeof elem.getBoundingClientRect !== strundefined ) {
107
                        box = elem.getBoundingClientRect();
108
                }
109
                win = getWindow( doc );
110
                return {
111
                        top: box.top  + ( win.pageYOffset || docElem.scrollTop )  - ( docElem.clientTop  || 0 ),
112
                        left: box.left + ( win.pageXOffset || docElem.scrollLeft ) - ( docElem.clientLeft || 0 )
113
                };
114
        },
115

    
116
        position: function() {
117
                if ( !this[ 0 ] ) {
118
                        return;
119
                }
120

    
121
                var offsetParent, offset,
122
                        parentOffset = { top: 0, left: 0 },
123
                        elem = this[ 0 ];
124

    
125
                // fixed elements are offset from window (parentOffset = {top:0, left: 0}, because it is its only offset parent
126
                if ( jQuery.css( elem, "position" ) === "fixed" ) {
127
                        // we assume that getBoundingClientRect is available when computed position is fixed
128
                        offset = elem.getBoundingClientRect();
129
                } else {
130
                        // Get *real* offsetParent
131
                        offsetParent = this.offsetParent();
132

    
133
                        // Get correct offsets
134
                        offset = this.offset();
135
                        if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
136
                                parentOffset = offsetParent.offset();
137
                        }
138

    
139
                        // Add offsetParent borders
140
                        parentOffset.top  += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
141
                        parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
142
                }
143

    
144
                // Subtract parent offsets and element margins
145
                // note: when an element has margin: auto the offsetLeft and marginLeft
146
                // are the same in Safari causing offset.left to incorrectly be 0
147
                return {
148
                        top:  offset.top  - parentOffset.top - jQuery.css( elem, "marginTop", true ),
149
                        left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true)
150
                };
151
        },
152

    
153
        offsetParent: function() {
154
                return this.map(function() {
155
                        var offsetParent = this.offsetParent || docElem;
156

    
157
                        while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" ) && jQuery.css( offsetParent, "position" ) === "static" ) ) {
158
                                offsetParent = offsetParent.offsetParent;
159
                        }
160
                        return offsetParent || docElem;
161
                });
162
        }
163
});
164

    
165
// Create scrollLeft and scrollTop methods
166
jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
167
        var top = /Y/.test( prop );
168

    
169
        jQuery.fn[ method ] = function( val ) {
170
                return access( this, function( elem, method, val ) {
171
                        var win = getWindow( elem );
172

    
173
                        if ( val === undefined ) {
174
                                return win ? (prop in win) ? win[ prop ] :
175
                                        win.document.documentElement[ method ] :
176
                                        elem[ method ];
177
                        }
178

    
179
                        if ( win ) {
180
                                win.scrollTo(
181
                                        !top ? val : jQuery( win ).scrollLeft(),
182
                                        top ? val : jQuery( win ).scrollTop()
183
                                );
184

    
185
                        } else {
186
                                elem[ method ] = val;
187
                        }
188
                }, method, val, arguments.length, null );
189
        };
190
});
191

    
192
// Add the top/left cssHooks using jQuery.fn.position
193
// Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
194
// getComputedStyle returns percent when specified for top/left/bottom/right
195
// rather than make the css module depend on the offset module, we just check for it here
196
jQuery.each( [ "top", "left" ], function( i, prop ) {
197
        jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
198
                function( elem, computed ) {
199
                        if ( computed ) {
200
                                computed = curCSS( elem, prop );
201
                                // if curCSS returns percentage, fallback to offset
202
                                return rnumnonpx.test( computed ) ?
203
                                        jQuery( elem ).position()[ prop ] + "px" :
204
                                        computed;
205
                        }
206
                }
207
        );
208
});
209

    
210
return jQuery;
211
});