2 * jQuery UI Position 1.8.10
4 * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
5 * Dual licensed under the MIT or GPL Version 2 licenses.
6 * http://jquery.org/license
8 * http://docs.jquery.com/UI/Position
10 (function( $, undefined ) {
14 var horizontalPositions = /left|center|right/,
15 verticalPositions = /top|center|bottom/,
17 _position = $.fn.position,
18 _offset = $.fn.offset;
20 $.fn.position = function( options ) {
21 if ( !options || !options.of ) {
22 return _position.apply( this, arguments );
25 // make a copy, we don't want to modify arguments
26 options = $.extend( {}, options );
28 var target = $( options.of ),
29 targetElem = target[0],
30 collision = ( options.collision || "flip" ).split( " " ),
31 offset = options.offset ? options.offset.split( " " ) : [ 0, 0 ],
36 if ( targetElem.nodeType === 9 ) {
37 targetWidth = target.width();
38 targetHeight = target.height();
39 basePosition = { top: 0, left: 0 };
40 // TODO: use $.isWindow() in 1.9
41 } else if ( targetElem.setTimeout ) {
42 targetWidth = target.width();
43 targetHeight = target.height();
44 basePosition = { top: target.scrollTop(), left: target.scrollLeft() };
45 } else if ( targetElem.preventDefault ) {
46 // force left top to allow flipping
47 options.at = "left top";
48 targetWidth = targetHeight = 0;
49 basePosition = { top: options.of.pageY, left: options.of.pageX };
51 targetWidth = target.outerWidth();
52 targetHeight = target.outerHeight();
53 basePosition = target.offset();
56 // force my and at to have valid horizontal and veritcal positions
57 // if a value is missing or invalid, it will be converted to center
58 $.each( [ "my", "at" ], function() {
59 var pos = ( options[this] || "" ).split( " " );
60 if ( pos.length === 1) {
61 pos = horizontalPositions.test( pos[0] ) ?
62 pos.concat( [center] ) :
63 verticalPositions.test( pos[0] ) ?
64 [ center ].concat( pos ) :
67 pos[ 0 ] = horizontalPositions.test( pos[0] ) ? pos[ 0 ] : center;
68 pos[ 1 ] = verticalPositions.test( pos[1] ) ? pos[ 1 ] : center;
69 options[ this ] = pos;
72 // normalize collision option
73 if ( collision.length === 1 ) {
74 collision[ 1 ] = collision[ 0 ];
77 // normalize offset option
78 offset[ 0 ] = parseInt( offset[0], 10 ) || 0;
79 if ( offset.length === 1 ) {
80 offset[ 1 ] = offset[ 0 ];
82 offset[ 1 ] = parseInt( offset[1], 10 ) || 0;
84 if ( options.at[0] === "right" ) {
85 basePosition.left += targetWidth;
86 } else if ( options.at[0] === center ) {
87 basePosition.left += targetWidth / 2;
90 if ( options.at[1] === "bottom" ) {
91 basePosition.top += targetHeight;
92 } else if ( options.at[1] === center ) {
93 basePosition.top += targetHeight / 2;
96 basePosition.left += offset[ 0 ];
97 basePosition.top += offset[ 1 ];
99 return this.each(function() {
100 var elem = $( this ),
101 elemWidth = elem.outerWidth(),
102 elemHeight = elem.outerHeight(),
103 marginLeft = parseInt( $.curCSS( this, "marginLeft", true ) ) || 0,
104 marginTop = parseInt( $.curCSS( this, "marginTop", true ) ) || 0,
105 collisionWidth = elemWidth + marginLeft +
106 ( parseInt( $.curCSS( this, "marginRight", true ) ) || 0 ),
107 collisionHeight = elemHeight + marginTop +
108 ( parseInt( $.curCSS( this, "marginBottom", true ) ) || 0 ),
109 position = $.extend( {}, basePosition ),
112 if ( options.my[0] === "right" ) {
113 position.left -= elemWidth;
114 } else if ( options.my[0] === center ) {
115 position.left -= elemWidth / 2;
118 if ( options.my[1] === "bottom" ) {
119 position.top -= elemHeight;
120 } else if ( options.my[1] === center ) {
121 position.top -= elemHeight / 2;
124 // prevent fractions (see #5280)
125 position.left = Math.round( position.left );
126 position.top = Math.round( position.top );
128 collisionPosition = {
129 left: position.left - marginLeft,
130 top: position.top - marginTop
133 $.each( [ "left", "top" ], function( i, dir ) {
134 if ( $.ui.position[ collision[i] ] ) {
135 $.ui.position[ collision[i] ][ dir ]( position, {
136 targetWidth: targetWidth,
137 targetHeight: targetHeight,
138 elemWidth: elemWidth,
139 elemHeight: elemHeight,
140 collisionPosition: collisionPosition,
141 collisionWidth: collisionWidth,
142 collisionHeight: collisionHeight,
150 if ( $.fn.bgiframe ) {
153 elem.offset( $.extend( position, { using: options.using } ) );
159 left: function( position, data ) {
160 var win = $( window ),
161 over = data.collisionPosition.left + data.collisionWidth - win.width() - win.scrollLeft();
162 position.left = over > 0 ? position.left - over : Math.max( position.left - data.collisionPosition.left, position.left );
164 top: function( position, data ) {
165 var win = $( window ),
166 over = data.collisionPosition.top + data.collisionHeight - win.height() - win.scrollTop();
167 position.top = over > 0 ? position.top - over : Math.max( position.top - data.collisionPosition.top, position.top );
172 left: function( position, data ) {
173 if ( data.at[0] === center ) {
176 var win = $( window ),
177 over = data.collisionPosition.left + data.collisionWidth - win.width() - win.scrollLeft(),
178 myOffset = data.my[ 0 ] === "left" ?
180 data.my[ 0 ] === "right" ?
183 atOffset = data.at[ 0 ] === "left" ?
186 offset = -2 * data.offset[ 0 ];
187 position.left += data.collisionPosition.left < 0 ?
188 myOffset + atOffset + offset :
190 myOffset + atOffset + offset :
193 top: function( position, data ) {
194 if ( data.at[1] === center ) {
197 var win = $( window ),
198 over = data.collisionPosition.top + data.collisionHeight - win.height() - win.scrollTop(),
199 myOffset = data.my[ 1 ] === "top" ?
201 data.my[ 1 ] === "bottom" ?
204 atOffset = data.at[ 1 ] === "top" ?
207 offset = -2 * data.offset[ 1 ];
208 position.top += data.collisionPosition.top < 0 ?
209 myOffset + atOffset + offset :
211 myOffset + atOffset + offset :
217 // offset setter from jQuery 1.4
218 if ( !$.offset.setOffset ) {
219 $.offset.setOffset = function( elem, options ) {
220 // set position first, in-case top/left are set even on static elem
221 if ( /static/.test( $.curCSS( elem, "position" ) ) ) {
222 elem.style.position = "relative";
224 var curElem = $( elem ),
225 curOffset = curElem.offset(),
226 curTop = parseInt( $.curCSS( elem, "top", true ), 10 ) || 0,
227 curLeft = parseInt( $.curCSS( elem, "left", true ), 10) || 0,
229 top: (options.top - curOffset.top) + curTop,
230 left: (options.left - curOffset.left) + curLeft
233 if ( 'using' in options ) {
234 options.using.call( elem, props );
236 curElem.css( props );
240 $.fn.offset = function( options ) {
241 var elem = this[ 0 ];
242 if ( !elem || !elem.ownerDocument ) { return null; }
244 return this.each(function() {
245 $.offset.setOffset( this, options );
248 return _offset.call( this );