3

我在定义 和 的元素上left调用rightgetComputedStyle bottom

在 Chrome中,它'auto'作为值返回,top但在 Firefox 中,它返回像素值,但是如果您查看 Firefox 中的检查器,则该top值不会显示在计算窗格中。

有没有办法解决这个问题?这是一个显示问题的小提琴http://jsfiddle.net/DEfusion/9NaGD/

4

2 回答 2

1

老问题,但我也遇到了这个问题并得到了解决方案,正如 Prisoner 所说,Firefox 对某些属性的处理方式略有不同,它将返回使用的值(它将自动转换为像素)而不是解析的值。幸运的是,自 Firefox 19 以来,有一个名为 getDefaultComputedStyle 的解决方案,仅在 Firefox 中支持并返回解析的值。

一种方法是测试浏览器,如果是 Firefox,则使用 getDefaultComputedStyle,否则使用 getComputedStyle

于 2015-02-13T08:10:01.300 回答
1

通过:https ://developer.mozilla.org/en/docs/Web/API/window.getComputedStyle

在 Firefox 中,值为 auto 的属性返回使用的值,而不是值 auto。所以如果你申请 top:auto; 和底部:0;在 height:30px 且其包含块为 height:100px 的元素上;在请求 top 的计算样式时,Firefox 将返回 top:70px,为 100px-30px=70px。

于 2014-05-12T17:09:18.920 回答