【问题标题】:Firefox getComputedStyle returns "auto"Firefox getComputedStyle 返回“自动”
【发布时间】:2015-11-13 03:10:50
【问题描述】:

这个问题之前有人问过“高度”和“宽度”属性,但我没有找到可行的解决方案。

Firefox documentation 说:

在 Firefox 中,值为 auto 的属性返回使用的值,而不是值 auto。

但是,window.getComputedStyle(element).getPropertyValue("top") 返回 "auto" 而不是像素值。有人知道解决方法吗?

Ubuntu 15.04 上的 Firefox 42.0

【问题讨论】:

标签: javascript firefox


【解决方案1】:

看起来你的元素有position: static。任何其他类型的定位似乎都可以正确计算。这是一个静态元素的测试:

document.getElementById('results').innerHTML = window.getComputedStyle(document.getElementById('element')).top;
#element {
  top : auto;
  }
<div id="element"></div>
<pre id="results"></pre>

这是position: relative的另一个:

document.getElementById('results').innerHTML = window.getComputedStyle(document.getElementById('element')).top;
#element {
  position: relative;
  top : auto;
  }
<div id="element"></div>
<pre id="results"></pre>

top 应该是 static 元素的无操作。所以在这种情况下使用 Rect 方法可能会更好。

【讨论】:

  • 此修复不适用于 ie。值得一提。虽然很聪明。
猜你喜欢
  • 1970-01-01
  • 2018-11-07
  • 2012-11-07
  • 2011-11-05
  • 2012-07-01
  • 2012-01-27
  • 2014-08-09
  • 2017-11-03
  • 1970-01-01
相关资源
最近更新 更多