Skip to content

Commit c958b21

Browse files
committed
Position: Handle decimal percentage offsets. Fixes #9076: percentage offset does not support decimal
1 parent ab408c9 commit c958b21

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

tests/unit/position/position_core.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ test( "of", function() {
221221
});
222222

223223
test( "offsets", function() {
224-
expect( 4 );
224+
expect( 7 );
225225

226226
$( "#elx" ).position({
227227
my: "left top",
@@ -254,6 +254,30 @@ test( "offsets", function() {
254254
collision: "none"
255255
});
256256
deepEqual( $( "#elx" ).offset(), { top: 65, left: 37 }, "percentage offsets in my" );
257+
258+
$( "#elx" ).position({
259+
my: "left-30.001% top+50.0%",
260+
at: "left bottom",
261+
of: "#parentx",
262+
collision: "none"
263+
});
264+
deepEqual( $( "#elx" ).offset(), { top: 65, left: 37 }, "decimal percentage offsets in my" );
265+
266+
$( "#elx" ).position({
267+
my: "left+10.4 top-10.6",
268+
at: "left bottom",
269+
of: "#parentx",
270+
collision: "none"
271+
});
272+
deepEqual( $( "#elx" ).offset(), { top: 49, left: 50 }, "decimal offsets in my" );
273+
274+
$( "#elx" ).position({
275+
my: "left+right top-left",
276+
at: "left-top bottom-bottom",
277+
of: "#parentx",
278+
collision: "none"
279+
});
280+
deepEqual( $( "#elx" ).offset(), { top: 60, left: 40 }, "invalid offsets" );
257281
});
258282

259283
test( "using", function() {

ui/jquery.ui.position.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ var cachedScrollbarWidth,
1818
round = Math.round,
1919
rhorizontal = /left|center|right/,
2020
rvertical = /top|center|bottom/,
21-
roffset = /[\+\-]\d+%?/,
21+
roffset = /[\+\-]\d+(\.[\d]+)?%?/,
2222
rposition = /^\w+/,
2323
rpercent = /%$/,
2424
_position = $.fn.position;
2525

2626
function getOffsets( offsets, width, height ) {
2727
return [
28-
parseInt( offsets[ 0 ], 10 ) * ( rpercent.test( offsets[ 0 ] ) ? width / 100 : 1 ),
29-
parseInt( offsets[ 1 ], 10 ) * ( rpercent.test( offsets[ 1 ] ) ? height / 100 : 1 )
28+
parseFloat( offsets[ 0 ] ) * ( rpercent.test( offsets[ 0 ] ) ? width / 100 : 1 ),
29+
parseFloat( offsets[ 1 ] ) * ( rpercent.test( offsets[ 1 ] ) ? height / 100 : 1 )
3030
];
3131
}
3232

0 commit comments

Comments
 (0)