jQuery returning different heights for the same objects in WebKit (chrome) and Firefox

后端 未结 4 1828
野性不改
野性不改 2021-02-02 17:45

Im perplexed by this one.

jQuery.height() is coming back with different values in Firefox and Chrome. Measuring the pixels on-screen indicates that of the two, Chrome ap

4条回答
  •  攒了一身酷
    2021-02-02 18:25

    Firefox and in general, Gecko based browsers differ from others in that they try to do subpixel layout and rendering.
    This can make life hard especially if you work with table cells that are sized based on their contents.
    IE, Webkit and Gecko can report different dimensions - the latter reporting some exotic fractional sizes as well.
    As for the bug: after struggling with something similar - measuring dimensions of dynamically sized table cells - for a while, I had ended up special casing for fractional numbers.

    As it influences positions as well as dimensions, some people ended up using clientWidth instead of jQuery's 'innerWidth()', while some just use parseInt() on the returned results. (Search for fract in the sources.)

    If that is not satisfactory, you may try the following:

    • render the primary table with "visibility:hidden" by default so that it is laid out but not displayed
    • measure the cell heights from jQuery
    • round the heights to the nearest integer value in the primary table and apply as an inline style (rounding up may be a good idea to ensure that cells don't cut contents by 1px)
    • render the secondary table based on these integer values and restore visibility of primary table

    It might still occur that the borders are misaligned - this can be the case if the position of the primary and secondary tables are not the same (e.g.: primary top: 123.75px, secondary top: 123px.) In that case, placing the tables in a common container or just repositioning to integer coordinates may help.

    Good luck...

    Edit: As far as I remember, because of some IE and border-collapse anomalies, I resorted to the (deprecated) cellspacing property - that should be placed on the table element into HTML, not CSS - and specified separate borders with a dimension of 0 in CSS. I placed the whole table in a div with a nice background-color.
    The whole thing had the effect as if the table borders were 1px, collapsed having the color of the background div....

提交回复
热议问题