From a4ab6ccf1ce1d0027639e3331358697aadd11e75 Mon Sep 17 00:00:00 2001 From: kborchers Date: Wed, 14 Sep 2011 13:53:15 -0500 Subject: [PATCH] Position: Added a check for fraction support in element positions based on @inukshuk's original method --- ui/jquery.ui.position.js | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/ui/jquery.ui.position.js b/ui/jquery.ui.position.js index 24a033fc212..314fae1cf2a 100644 --- a/ui/jquery.ui.position.js +++ b/ui/jquery.ui.position.js @@ -17,6 +17,7 @@ var rhorizontal = /left|center|right/, rposition = /^\w+/, rpercent = /%$/, center = "center", + support = {}, _position = $.fn.position; $.position = { @@ -183,6 +184,11 @@ $.fn.position = function( options ) { position.left += myOffset[ 0 ]; position.top += myOffset[ 1 ]; + if ( !support.fractions ) { + position.left = Math.round( position.left ); + position.top = Math.round( position.top ); + } + collisionPosition = { marginLeft: marginLeft, marginTop: marginTop @@ -406,6 +412,49 @@ $.ui.position = { } }; +// fraction support test (older versions of jQuery and certain browsers 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 ( 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.0 && offsetTotal < 22; +})(); + // DEPRECATED if ( $.uiBackCompat !== false ) { // offset option