Skip to content

Commit 4b9d5d1

Browse files
bhollisscottgonzalez
authored andcommitted
Position: Take margin into account when performing collisions. Fixes #5766 - position: collision should take margin into account.
1 parent 0a0a39f commit 4b9d5d1

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

tests/unit/position/position_core.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,58 @@ test("collision: none, with offset", function() {
354354
}, { top: -13, left: -12 }, "left top, negative offset");
355355
});
356356

357+
test("collision: fit, with margin", function() {
358+
$("#elx").css("margin", 10);
359+
360+
collisionTest({
361+
collision: "fit"
362+
}, { top: $(window).height() - 20, left: $(window).width() - 20 }, "right bottom");
363+
364+
collisionTest2({
365+
collision: "fit"
366+
}, { top: 10, left: 10 }, "left top");
367+
368+
$("#elx").css({
369+
"margin-left": 5,
370+
"margin-top": 5
371+
});
372+
373+
collisionTest({
374+
collision: "fit"
375+
}, { top: $(window).height() - 20, left: $(window).width() - 20 }, "right bottom");
376+
377+
collisionTest2({
378+
collision: "fit"
379+
}, { top: 5, left: 5 }, "left top");
380+
381+
$("#elx").css({
382+
"margin-right": 15,
383+
"margin-bottom": 15
384+
});
385+
386+
collisionTest({
387+
collision: "fit"
388+
}, { top: $(window).height() - 25, left: $(window).width() - 25 }, "right bottom");
389+
390+
collisionTest2({
391+
collision: "fit"
392+
}, { top: 5, left: 5 }, "left top");
393+
});
394+
395+
test("collision: flip, with margin", function() {
396+
$("#elx").css("margin", 10);
397+
398+
collisionTest({
399+
collision: "flip",
400+
at: "left top"
401+
}, { top: $(window).height() - 10, left: $(window).width() - 10 }, "left top");
402+
403+
collisionTest2({
404+
collision: "flip",
405+
at: "right bottom"
406+
}, { top: 0, left: 0 }, "right bottom");
407+
});
408+
357409
//test('bug #5280: consistent results (avoid fractional values)', function() {
358410
// var wrapper = $('#bug-5280'),
359411
// elem = wrapper.children(),

ui/jquery.ui.position.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ $.fn.position = function( options ) {
9999
var elem = $( this ),
100100
elemWidth = elem.outerWidth(),
101101
elemHeight = elem.outerHeight(),
102-
position = $.extend( {}, basePosition );
102+
marginLeft = parseInt( $.curCSS( this, "marginLeft", true ) ) || 0,
103+
marginTop = parseInt( $.curCSS( this, "marginTop", true ) ) || 0,
104+
collisionWidth = elemWidth + marginLeft +
105+
parseInt( $.curCSS( this, "marginRight", true ) ) || 0,
106+
collisionHeight = elemHeight + marginTop +
107+
parseInt( $.curCSS( this, "marginBottom", true ) ) || 0,
108+
position = $.extend( {}, basePosition ),
109+
collisionPosition;
103110

104111
if ( options.my[0] === "right" ) {
105112
position.left -= elemWidth;
@@ -117,13 +124,21 @@ $.fn.position = function( options ) {
117124
position.left = parseInt( position.left );
118125
position.top = parseInt( position.top );
119126

127+
collisionPosition = {
128+
left: position.left - marginLeft,
129+
top: position.top - marginTop
130+
};
131+
120132
$.each( [ "left", "top" ], function( i, dir ) {
121133
if ( $.ui.position[ collision[i] ] ) {
122134
$.ui.position[ collision[i] ][ dir ]( position, {
123135
targetWidth: targetWidth,
124136
targetHeight: targetHeight,
125137
elemWidth: elemWidth,
126138
elemHeight: elemHeight,
139+
collisionPosition: collisionPosition,
140+
collisionWidth: collisionWidth,
141+
collisionHeight: collisionHeight,
127142
offset: offset,
128143
my: options.my,
129144
at: options.at
@@ -142,13 +157,13 @@ $.ui.position = {
142157
fit: {
143158
left: function( position, data ) {
144159
var win = $( window ),
145-
over = position.left + data.elemWidth - win.width() - win.scrollLeft();
146-
position.left = over > 0 ? position.left - over : Math.max( 0, position.left );
160+
over = data.collisionPosition.left + data.collisionWidth - win.width() - win.scrollLeft();
161+
position.left = over > 0 ? position.left - over : Math.max( position.left - data.collisionPosition.left, position.left );
147162
},
148163
top: function( position, data ) {
149164
var win = $( window ),
150-
over = position.top + data.elemHeight - win.height() - win.scrollTop();
151-
position.top = over > 0 ? position.top - over : Math.max( 0, position.top );
165+
over = data.collisionPosition.top + data.collisionHeight - win.height() - win.scrollTop();
166+
position.top = over > 0 ? position.top - over : Math.max( position.top - data.collisionPosition.top, position.top );
152167
}
153168
},
154169

@@ -158,7 +173,7 @@ $.ui.position = {
158173
return;
159174
}
160175
var win = $( window ),
161-
over = position.left + data.elemWidth - win.width() - win.scrollLeft(),
176+
over = data.collisionPosition.left + data.collisionWidth - win.width() - win.scrollLeft(),
162177
myOffset = data.my[ 0 ] === "left" ?
163178
-data.elemWidth :
164179
data.my[ 0 ] === "right" ?
@@ -168,7 +183,7 @@ $.ui.position = {
168183
data.targetWidth :
169184
-data.targetWidth,
170185
offset = -2 * data.offset[ 0 ];
171-
position.left += position.left < 0 ?
186+
position.left += data.collisionPosition.left < 0 ?
172187
myOffset + atOffset + offset :
173188
over > 0 ?
174189
myOffset + atOffset + offset :
@@ -179,7 +194,7 @@ $.ui.position = {
179194
return;
180195
}
181196
var win = $( window ),
182-
over = position.top + data.elemHeight - win.height() - win.scrollTop(),
197+
over = data.collisionPosition.top + data.collisionHeight - win.height() - win.scrollTop(),
183198
myOffset = data.my[ 1 ] === "top" ?
184199
-data.elemHeight :
185200
data.my[ 1 ] === "bottom" ?
@@ -189,7 +204,7 @@ $.ui.position = {
189204
data.targetHeight :
190205
-data.targetHeight,
191206
offset = -2 * data.offset[ 1 ];
192-
position.top += position.top < 0 ?
207+
position.top += data.collisionPosition.top < 0 ?
193208
myOffset + atOffset + offset :
194209
over > 0 ?
195210
myOffset + atOffset + offset :

0 commit comments

Comments
 (0)