Skip to content

Commit c11ac9c

Browse files
author
David Murdoch
committed
Now takes scrollbar width into account in collision detection
1 parent e4a4299 commit c11ac9c

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

ui/jquery.ui.position.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,32 @@ var rhorizontal = /left|center|right/,
1717
rposition = /^\w+/,
1818
rpercent = /%$/,
1919
center = "center",
20-
_position = $.fn.position;
20+
_position = $.fn.position,
21+
getScrollbarWidth = function() {
22+
var div = $( "<div style='display:block;width:50px;height:50px;overflow:hidden;'><div style='height:100px;width:auto;'></div></div>" ),
23+
innerDiv = div.children()[0],
24+
w1, w2;
25+
$( "body" ).append( div );
26+
w1 = innerDiv.offsetWidth;
27+
div.css( "overflow", "scroll" );
28+
29+
w2 = innerDiv.offsetWidth;
30+
31+
if ( w1 === w2 ) {
32+
w2 = div[0].clientWidth;
33+
}
34+
35+
div.remove();
36+
37+
return w1 - w2;
38+
},
39+
getScrollInfo = function( within ) {
40+
var that = within[0],
41+
scrollHeight = within.height() < that.scrollHeight,
42+
scrollWidth = within.width() < that.scrollWidth,
43+
scrollbarWidth = getScrollbarWidth();
44+
return { height : scrollHeight ? scrollbarWidth : 0, width : scrollWidth ? scrollbarWidth : 0 };
45+
};
2146

2247
$.fn.position = function( options ) {
2348
if ( !options || !options.of ) {
@@ -114,16 +139,17 @@ $.fn.position = function( options ) {
114139
basePosition.left += atOffset[ 0 ];
115140
basePosition.top += atOffset[ 1 ];
116141

117-
return this.each(function() {
142+
return this.each(function() { console.log(getScrollInfo( within ));
118143
var elem = $( this ),
119144
elemWidth = elem.outerWidth(),
120145
elemHeight = elem.outerHeight(),
121146
marginLeft = parseInt( $.curCSS( this, "marginLeft", true ) ) || 0,
122147
marginTop = parseInt( $.curCSS( this, "marginTop", true ) ) || 0,
148+
scrollInfo = getScrollInfo( within ),
123149
collisionWidth = elemWidth + marginLeft +
124-
( parseInt( $.curCSS( this, "marginRight", true ) ) || 0 ),
150+
( parseInt( $.curCSS( this, "marginRight", true ) ) || 0 ) + scrollInfo.width,
125151
collisionHeight = elemHeight + marginTop +
126-
( parseInt( $.curCSS( this, "marginBottom", true ) ) || 0 ),
152+
( parseInt( $.curCSS( this, "marginBottom", true ) ) || 0 ) + scrollInfo.height,
127153
position = $.extend( {}, basePosition ),
128154
myOffset = [
129155
parseInt( offsets.my[ 0 ], 10 ) *

0 commit comments

Comments
 (0)