Non integer offset positions in jQuery

蓝咒 提交于 2019-12-12 08:39:52

问题


jQuerys offset function sometimes returns rational numbers (like 12.645613) for top or left. I thought that top and left positions are in pixels and so should be integers (there are no half pixel, or?).


回答1:


Top and left positions can be floating point numbers with any of the units cm, mm, in, pt, pc, em, ex or px, or percentages.

Example:

.someElement { top: 42%; left: 3.14in; }

The offset function returns the position translated into pixels, so that can very well be a floating point number. The values are not rounded.

With the example given, if the height of the parent would for example be 32 pixels, the top value of the element would be 32 * 0.42 = 13.44 pixels.




回答2:


This is an old question, but I guess I could post an answer here, for posterity.

I was having this issue too, and I found out (thanks Firebug) that the problem lies in that the h1 and h3 tags have floating point margins (for instance, Firefox by default gives <H1> a 0.67em 0 margin).

When you delete the <H1> and <H3> tags $(".fixedColumn").eq(0).offset().top returns an integer. You could just specify margins for those tags and the problem should be fixed.



来源:https://stackoverflow.com/questions/4626479/non-integer-offset-positions-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!