简体   繁体   English

获取元素解析的高度 CSS 属性值; 认识 100%

[英]Get element's resolved height CSS property value; recognize 100%

The values returned by getComputedStyle are resolved values . getComputedStyle返回的值是解析值 These are usually the same as CSS 2.1's computed values, but for some older properties like width , height , or padding , they are instead the same as used values .这些通常与 CSS 2.1 的计算值相同,但对于一些较旧的属性,如widthheightpadding ,它们与used values相同。

-- MDN: window.getComputedStyle() notes -- MDN: window.getComputedStyle() 注释

So is it currently possible to get the resolved value of height as it was specified in stylesheet?那么目前是否可以获得样式表中指定的height解析值

Ie know that some element's computed ..px (= used ) height was specified as height: 100%;即知道某些元素的计算..px (= used ) 高度被指定为height: 100%; in style sheet?在样式表中? ( 100% being the resolved value in question.) 100%是有问题的已解决值。)

Is there some new specification regarding this problem in consideration?是否有一些关于这个问题的新规范正在考虑中?


Edit 2020-08-17: see very similar question and excellent answer from 2012: Is there a cross-browser method of getting the used css values of all properties of all elements?编辑 2020-08-17:看到非常相似的问题和 2012 年的优秀答案:是否有一种跨浏览器方法来获取所有元素的所有属性的已用 css 值? (Sadly, noting seem to ave changed since then.) (可悲的是,从那时起,注释似乎发生了变化。)

No, there is no specification or functionality that supports or enables this method.不,没有支持或启用此方法的规范或功能。 There are plenty of ways to go the other direction, including...有很多方法可以走向另一个方向,包括......

... but none that will retrieve the exact percentage specified in the CSS, unfortunately. ...但不幸的是,没有一个可以检索 CSS 中指定的确切百分比。

You can try , as I've done below.你可以试试,就像我在下面所做的那样。


RegEx正则表达式

If your height is specified as an inline style, you could RegEx it out of the attribute like so 1 :如果您的高度被指定为内联样式,您可以像这样1一样将其从属性中正则表达式:

 let elem = document.getElementsByTagName("div")[0]; let re = /(?<=\\height:\\s+?)[^\\;]+/i; console.log(re.exec(elem.getAttribute("style"))[0]);
 <div style="color: black; height: 100%; background: white;"></div>

This is terrible practice, though, and could be janky if there are multiple width declarations (which should never happen, but we're already in "bad code land", so why not?).但是,这是一种糟糕的做法,如果有多个宽度声明(这应该永远不会发生,但我们已经处于“糟糕的代码领域”,为什么不呢?)。 Of course, this ignores the fact that inline styles should generally be avoided in the first place, so this probably won't apply to you.当然,这忽略了一个事实,即通常应该首先避免内联样式,因此这可能不适用于您。


Bounding calculations边界计算

It's also possible to calculate the value yourself by comparing the height of the element with the height of its parent.也可以通过将元素的高度与其父元素的高度进行比较来自己计算值。

 let elem = document.getElementById("inner"); let pare = elem.parentElement; let hrat = `${100*(elem.offsetHeight / pare.offsetHeight)}%`; console.log(hrat);
 #container { height: 300px; width: 400px; background: repeating-linear-gradient(45deg, rgba(255,0,0,0.35), rgba(255,0,0,0.35) 10px, rgba(255,0,0,0.1) 10px, rgba(255,0,0,0.1) 20px), linear-gradient(white, white); } #inner { height: 200px; width: 400px; background: darkgreen; mix-blend-mode: hue; }
 <div id="container"> <div id="inner"></div> </div>

If you add borders, margin, or padding, or the element adapts to the size of its content, though, this calculation will be incorrect.但是,如果添加边框、边距或填充,或者元素适应其内容的大小,则此计算将不正确。


Conclusion结论

In conclusion, everything is jank.总而言之,一切都是垃圾。

In my opinion, you'd be better off not fighting with CSS and JavaScript to coerce the value from the available information, and working out a way to do without the value.在我看来,最好不要使用 CSS 和 JavaScript 来强制从可用信息中获取值,而是想出一种方法来消除这些值。 I've tried to do this kind of thing many times, so be forewarned: this way lies madness.我已经多次尝试做这种事情,所以请预先警告:这种方式是疯狂的。


1 RegEx lookbehinds are not even remotely close to being fully supported, so shouldn't be used in production. 1 RegEx 后视甚至远未完全支持,因此不应在生产中使用。

You could read the stylesheet rule itself.您可以阅读样式表规则本身。 If you know the selector that sets an elements width/height you can do this:如果您知道设置元素宽度/高度的选择器,您可以这样做:

.box {
  width: 12vw;
  background: red;  
}
<div class="box">red</div>
var sheet = document.styleSheets[0];
var rules = sheet.rules;

for (var i = 0; i < rules.length; i++) {
  if (rules[i].selectorText == '.box') {
    console.log(rules[i].style.width);
  }  
}

This will give you the 12vw you're looking for.这将为您12vw您正在寻找的12vw

Depending on how your elements are defined, you could in theory create a helper function that gets these values for you by looping through the elements classList .根据元素的定义方式,理论上您可以创建一个辅助函数,通过循环遍历元素classList为您获取这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM