[英]window.getComputedStyle always returns measurement in pixels
I am just wondering why, for instance, windows.getComputedStyle(element).top
always returns the measurement in pixels, even in cases whereby the top
position is set explicitly to %
like so element.style.top = 25 + '%'
.我只是想知道为什么,例如,
windows.getComputedStyle(element).top
总是以像素为单位返回测量值,即使在top
位置显式设置为%
情况下也是如此element.style.top = 25 + '%'
。
I have not been able to find any information online.我一直无法在网上找到任何信息。 Anybody know why this is the case?!
有谁知道为什么会这样?!
What you're looking for is element.style.top
.您正在寻找的是
element.style.top
。
That is the intended behaviour of window.getComputedStyle
.这是
window.getComputedStyle
的预期行为。
"The window.getComputedStyle()
method returns an object containing the values of all CSS properties of an element, after applying active stylesheets and resolving any basic computation those values may contain." “在应用活动样式表并解析这些值可能包含的任何基本计算之后,
window.getComputedStyle()
方法返回一个包含元素所有 CSS 属性值的对象。”
Meaning that everything that is a unit other than pixels will be calculated or processed and then presented as pixels.这意味着除像素以外的所有单位都将被计算或处理,然后以像素的形式呈现。 Even the name GetComputedStyle hints that.
甚至名称 GetComputedStyle 也暗示了这一点。
You can read more about this here if you want.如果您愿意,您可以在此处阅读有关此内容的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.