Is there a way to detect if the browser has subpixel precision?

后端 未结 4 382
失恋的感觉
失恋的感觉 2021-01-31 14:36

Is there a way to detect if the browser has subpixel precision for elements ?

IE9, unlike any of the other major browsers, has subpixel precision for its elements (an el

4条回答
  •  囚心锁ツ
    2021-01-31 15:19

    I'm not sure where you got the idea that IE9 is the only browser that supports fractional pixel units, but that assumption is totally incorrect.

    From section 4.3 of the spec (emphasis added):

    The format of a length value (denoted by in this specification) is a (with or without a decimal point) immediately followed by a unit identifier (e.g., px, em, etc.).

    And defining :

    Some value types may have integer values (denoted by ) or real number values (denoted by ). Real numbers and integers are specified in decimal notation only. An consists of one or more digits "0" to "9". A can either be an , or it can be zero or more digits followed by a dot (.) followed by one or more digits. Both integers and real numbers may be preceded by a "-" or "+" to indicate the sign. -0 is equivalent to 0 and is not a negative number.

    Therefore, per spec, the px length unit must support fractional numbers.

    To prove this, take a look at this fiddle in fullscreen and use your browser's zoom function to zoom all the way in:

    Fiddle screenshot

    In this Chrome screenshot, notice that the 5.5px blue box is indeed taller than the 5px red box.


    I think the confusion might stem from the fact that the non-standard element.clientHeight returns a calculated (rounded) integer value, and that rounding happens differently in different browsers.

    In my fiddle, for the clientHeight of the blue

    , IE9 and Firefox 15 at 100% zoom give 6. Chrome 22 and Opera 12 give 5. In all browsers, the value of that property changes as the user changes the browser's zoom level.

    In other words, it's unreliable.

    If you want to do calculations with the actual, fractional units of an element, use getComputedStyle.

    var el = $('#b')[0]; // the actual DOM element
    var height = parseFloat(getComputedStyle(el).height); // => 5.5
    

提交回复
热议问题