diff --git a/tests/unit/position/position_core.js b/tests/unit/position/position_core.js index f4075ccfe7c..b7d0894d386 100644 --- a/tests/unit/position/position_core.js +++ b/tests/unit/position/position_core.js @@ -406,22 +406,22 @@ test("collision: flip, with margin", function() { }, { top: 0, left: 0 }, "right bottom"); }); -//test('bug #5280: consistent results (avoid fractional values)', function() { -// var wrapper = $('#bug-5280'), -// elem = wrapper.children(), -// offset1 = elem.position({ -// my: 'center', -// at: 'center', -// of: wrapper, -// collision: 'none' -// }).offset(), -// offset2 = elem.position({ -// my: 'center', -// at: 'center', -// of: wrapper, -// collision: 'none' -// }).offset(); -// same(offset1, offset2); -//}); +test('bug #5280: consistent results (avoid fractional values)', function() { + var wrapper = $('#bug-5280'), + elem = wrapper.children(), + offset1 = elem.position({ + my: 'center', + at: 'center', + of: wrapper, + collision: 'none' + }).offset(), + offset2 = elem.position({ + my: 'center', + at: 'center', + of: wrapper, + collision: 'none' + }).offset(); + same(offset1, offset2); +}); })(jQuery); diff --git a/ui/jquery.ui.position.js b/ui/jquery.ui.position.js index 837f23f40cd..5cb609471d1 100644 --- a/ui/jquery.ui.position.js +++ b/ui/jquery.ui.position.js @@ -14,6 +14,7 @@ $.ui = $.ui || {}; var horizontalPositions = /left|center|right/, verticalPositions = /top|center|bottom/, center = "center", + support = {}, _position = $.fn.position, _offset = $.fn.offset; @@ -122,8 +123,10 @@ $.fn.position = function( options ) { } // prevent fractions (see #5280) - position.left = Math.round( position.left ); - position.top = Math.round( position.top ); + if (!support.fractions) { + position.left = Math.round( position.left ); + position.top = Math.round( position.top ); + } collisionPosition = { left: position.left - marginLeft, @@ -249,4 +252,28 @@ if ( !$.offset.setOffset ) { }; } +// support fractions (older versions of jquery don't support fractions) +$(function () { + var body = document.body, offset, + div = $(body.appendChild( document.createElement( "div" ) ) ); + + $.extend( div[0].style, { + position: 'absolute', + left: '10.2432222px', + top: '10.432325px', + height: '30px', + width: '201px' + }); + + offset = div.offset(); + div.offset(offset); + offset = div.offset(); + + // set display to none to avoid a layout bug in IE + // http://dev.jquery.com/ticket/4014 + body.removeChild( div[0] ).style.display = "none"; + + support.fractions = offset.top + offset.left > 20.0; +}); + }( jQuery ));