Skip to content

Commit 3970e8c

Browse files
committed
Position: Simplify fraction support test
Also makes the test lazy to avoid any potential layouts/recalculations during initialization. Fixes #9898 Ref #9899
1 parent dff1c74 commit 3970e8c

File tree

1 file changed

+21
-40
lines changed

1 file changed

+21
-40
lines changed

ui/position.js

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ var cachedScrollbarWidth, supportsOffsetFractions,
4141
rpercent = /%$/,
4242
_position = $.fn.position;
4343

44+
// Support: IE <=9 only
45+
supportsOffsetFractions = function() {
46+
var element = $( "<div>" )
47+
.css( "position", "absolute" )
48+
.appendTo( "body" )
49+
.offset( {
50+
top: 1.5,
51+
left: 1.5
52+
} ),
53+
support = element.offset().top === 1.5;
54+
55+
element.remove();
56+
57+
supportsOffsetFractions = function() {
58+
return support;
59+
};
60+
61+
return support;
62+
};
63+
4464
function getOffsets( offsets, width, height ) {
4565
return [
4666
parseFloat( offsets[ 0 ] ) * ( rpercent.test( offsets[ 0 ] ) ? width / 100 : 1 ),
@@ -243,7 +263,7 @@ $.fn.position = function( options ) {
243263
position.top += myOffset[ 1 ];
244264

245265
// if the browser doesn't support fractions, then round for consistent results
246-
if ( !supportsOffsetFractions ) {
266+
if ( !supportsOffsetFractions() ) {
247267
position.left = round( position.left );
248268
position.top = round( position.top );
249269
}
@@ -475,45 +495,6 @@ $.ui.position = {
475495
}
476496
};
477497

478-
// fraction support test
479-
( function() {
480-
var testElement, testElementParent, testElementStyle, offsetLeft, i,
481-
body = document.getElementsByTagName( "body" )[ 0 ],
482-
div = document.createElement( "div" );
483-
484-
//Create a "fake body" for testing based on method used in jQuery.support
485-
testElement = document.createElement( body ? "div" : "body" );
486-
testElementStyle = {
487-
visibility: "hidden",
488-
width: 0,
489-
height: 0,
490-
border: 0,
491-
margin: 0,
492-
background: "none"
493-
};
494-
if ( body ) {
495-
$.extend( testElementStyle, {
496-
position: "absolute",
497-
left: "-1000px",
498-
top: "-1000px"
499-
} );
500-
}
501-
for ( i in testElementStyle ) {
502-
testElement.style[ i ] = testElementStyle[ i ];
503-
}
504-
testElement.appendChild( div );
505-
testElementParent = body || document.documentElement;
506-
testElementParent.insertBefore( testElement, testElementParent.firstChild );
507-
508-
div.style.cssText = "position: absolute; left: 10.7432222px;";
509-
510-
offsetLeft = $( div ).offset().left;
511-
supportsOffsetFractions = offsetLeft > 10 && offsetLeft < 11;
512-
513-
testElement.innerHTML = "";
514-
testElementParent.removeChild( testElement );
515-
} )();
516-
517498
} )();
518499

519500
return $.ui.position;

0 commit comments

Comments
 (0)