开发者

element.offsetTop when parent has overflow and border

开发者 https://www.devze.com 2022-12-22 01:58 出处:网络
FF and IE give differents offsetTop when a child is into a parent with overflow and border. FF substracts the top border size of the parent from offsetTop value, so return a negative value(often); I t

FF and IE give differents offsetTop when a child is into a parent with overflow and border. FF substracts the top border size of the parent from offsetTop value, so return a negative value(often); I think IE return开发者_运维知识库 the correct value instead.

Test it

There is a crossbrowser way to get the same value?

Thank you.


The offsetTop property gives the position relative to the offset parent. The difference between Fx and IE is that they use different elements as offset parent in this case.

I don't know exactly how overflow affects this, and I haven't found anything about that in the definitions yet. However, there is a known bug in IE where it gets the offset parents wrong, and it may be a variation of that bug that you see here.


It appears to be a bug in firefox that has been open since 2005. The problem seems to be that there is no clear spec for offsetTop.

https://bugzilla.mozilla.org/show_bug.cgi?id=307502

Using getBoundingClientRect may work (it does for me).

var rect;
rect = el.getBoundingClientRect();
return rect.top;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号