简体   繁体   English

window.getComputedStyle 总是以像素为单位返回测量值

[英]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.

相关问题 Window.getComputedStyle()的结果是否始终是最新的? - Is the result of Window.getComputedStyle() always up-to-date? Javascript - window.getComputedStyle 返回“auto”作为元素的顶部和左侧属性 - Javascript - window.getComputedStyle returns “auto” as element top and left properties 使用 window.getComputedStyle 错误的 css 信息 - Wrong css informations by using window.getComputedStyle 如何修复Window.getComputedStyle不是对象错误 - How to fix Window.getComputedStyle is not an object error Window.getComputedStyle不显示内联样式 - Window.getComputedStyle does not show inline style 为什么 window.getComputedStyle(element).getPropertyValue("order") 不返回 flexbox 元素的顺序? - Why does window.getComputedStyle(element).getPropertyValue("order") doesn't returns order of a flexbox element? 为什么 window.getComputedStyle() 返回属性名称为“backgroundColor”的对象,但使用属性名称“background-color”也有效? - Why window.getComputedStyle() returns an object with a property name "backgroundColor" but using a property name "background-color" also works? 如何从window.getComputedStyle()或其他函数返回rgba值? - How to return rgba value from window.getComputedStyle() or other function? Window.getComputedStyle的参数1未实现接口Element - Argument 1 of Window.getComputedStyle does not implement interface Element window.getComputedStyle不适用于除Chrome之外的其他浏览器的速记属性 - window.getComputedStyle not working for shorthand properties in other browsers except Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM