Position: Added a check for fraction support in element positions. Fixes #7255 - Position: Revisit solution for off-by-1 errors.

This commit is contained in:
kborchers 2011-10-26 10:43:01 -04:00 committed by Scott González
parent 757384b8c5
commit bfbc0b1fb3
2 changed files with 66 additions and 20 deletions

View File

@ -406,22 +406,22 @@ test("collision: flip, with margin", function() {
}, { top: 0, left: 0 }, "right bottom"); }, { top: 0, left: 0 }, "right bottom");
}); });
//test('bug #5280: consistent results (avoid fractional values)', function() { test('bug #5280: consistent results (avoid fractional values)', function() {
// var wrapper = $('#bug-5280'), var wrapper = $('#bug-5280'),
// elem = wrapper.children(), elem = wrapper.children(),
// offset1 = elem.position({ offset1 = elem.position({
// my: 'center', my: 'center',
// at: 'center', at: 'center',
// of: wrapper, of: wrapper,
// collision: 'none' collision: 'none'
// }).offset(), }).offset(),
// offset2 = elem.position({ offset2 = elem.position({
// my: 'center', my: 'center',
// at: 'center', at: 'center',
// of: wrapper, of: wrapper,
// collision: 'none' collision: 'none'
// }).offset(); }).offset();
// same(offset1, offset2); same(offset1, offset2);
//}); });
})(jQuery); })(jQuery);

View File

@ -14,6 +14,7 @@ $.ui = $.ui || {};
var horizontalPositions = /left|center|right/, var horizontalPositions = /left|center|right/,
verticalPositions = /top|center|bottom/, verticalPositions = /top|center|bottom/,
center = "center", center = "center",
support = {},
_position = $.fn.position, _position = $.fn.position,
_offset = $.fn.offset; _offset = $.fn.offset;
@ -121,9 +122,11 @@ $.fn.position = function( options ) {
position.top -= elemHeight / 2; position.top -= elemHeight / 2;
} }
// prevent fractions (see #5280) // prevent fractions if jQuery version doesn't support them (see #5280)
position.left = Math.round( position.left ); if ( !support.fractions ) {
position.top = Math.round( position.top ); position.left = Math.round( position.left );
position.top = Math.round( position.top );
}
collisionPosition = { collisionPosition = {
left: position.left - marginLeft, left: position.left - marginLeft,
@ -249,4 +252,47 @@ if ( !$.offset.setOffset ) {
}; };
} }
// fraction support test (older versions of jQuery don't support fractions)
(function () {
var body = document.getElementsByTagName( "body" )[ 0 ],
div = document.createElement( "div" ),
testElement, testElementParent, testElementStyle, offset, offsetTotal;
//Create a "fake body" for testing based on method used in jQuery.support
testElement = document.createElement( body ? "div" : "body" );
testElementStyle = {
visibility: "hidden",
width: 0,
height: 0,
border: 0,
margin: 0,
background: "none"
};
if ( body ) {
jQuery.extend( testElementStyle, {
position: "absolute",
left: "-1000px",
top: "-1000px"
});
}
for ( var i in testElementStyle ) {
testElement.style[ i ] = testElementStyle[ i ];
}
testElement.appendChild( div );
testElementParent = body || document.documentElement;
testElementParent.insertBefore( testElement, testElementParent.firstChild );
div.style.cssText = "position: absolute; left: 10.7432222px; top: 10.432325px; height: 30px; width: 201px;";
offset = $( div ).offset( function( _, offset ) {
return offset;
}).offset();
testElement.innerHTML = "";
testElementParent.removeChild( testElement );
offsetTotal = offset.top + offset.left + ( body ? 2000 : 0 );
support.fractions = offsetTotal > 21 && offsetTotal < 22;
})();
}( jQuery )); }( jQuery ));