获取使用 calc 等表达式的 CSS 变量的计算值

FTh*_*son 11 javascript css css-variables

在 JavaScript 中,您可以使用 .css 获取 CSS 变量的值getPropertyValue(property)。此函数可用于检索:root块中声明的变量。

:root {
    --example-var: 50px;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果此变量表达式包含类似 的函数calc,则该getPropertyValue调用将表达式作为文本返回而不是计算它,即使使用getComputedStyle.

:root {
    --example-var: calc(100px - 5px);
}
Run Code Online (Sandbox Code Playgroud)

如何获取使用 CSS 函数的 CSS 变量的计算值calc

请参阅下面的示例:

:root {
    --example-var: 50px;
}
Run Code Online (Sandbox Code Playgroud)
:root {
    --example-var: calc(100px - 5px);
}
Run Code Online (Sandbox Code Playgroud)
let div = document.getElementById('example');
console.log(window.getComputedStyle(div).getPropertyValue('--example-var'))
Run Code Online (Sandbox Code Playgroud)

Tem*_*fif 6

从技术上讲,您不能,因为计算出的值不是静态的,将取决于其他属性。在这种情况下,这是微不足道的,因为我们正在处理像素值,但想象一下您将拥有百分比值的情况。百分比是相对于其他属性的,因此我们无法计算它,直到它与 一起使用var()。如果我们使用, 等单位em,则逻辑相同ch

下面是一个简单的例子来说明:

let div = document.getElementById('example');
console.log(window.getComputedStyle(div).getPropertyValue('--example-var'))
console.log(window.getComputedStyle(div).getPropertyValue('font-size'))
console.log(window.getComputedStyle(div).getPropertyValue('width'))
console.log(window.getComputedStyle(div).getPropertyValue('background-size'));
Run Code Online (Sandbox Code Playgroud)
:root {
  --example-var: calc(100% + 5px - 10px);
}
#example {
  font-size:var(--example-var);
  width:var(--example-var);
  background-size:var(--example-var);
}
Run Code Online (Sandbox Code Playgroud)
<div id='example'>some text</div>
Run Code Online (Sandbox Code Playgroud)

重要的是要注意最后一种情况,其中背景大小在组合百分比和像素值时具有特殊行为。您还可以清楚地看到,在第一种情况下,浏览器甚至不会计算5px - 10px并且只会在使用var().


如果您知道calc()将仅用于像素值,您可以简单地将其应用于任何属性并读取它。它会起作用,因为计算值将始终被评估为相同的属性:

let div = document.getElementById('example');
console.log(window.getComputedStyle(div).getPropertyValue('--example-var'))
console.log(window.getComputedStyle(div).getPropertyValue('font-size'))
console.log(window.getComputedStyle(div).getPropertyValue('width'))
console.log(window.getComputedStyle(div).getPropertyValue('background-size'));
console.log(window.getComputedStyle(div).getPropertyValue('background-color'));
Run Code Online (Sandbox Code Playgroud)
:root {
  --example-var: calc(100px - 10px);
  --test:var(--example-var)
}
#example {
  font-size:var(--example-var);
  width:var(--example-var);
  background-size:var(--example-var);
  background-color:var(--example-var);
}
Run Code Online (Sandbox Code Playgroud)
<div id='example'></div>
Run Code Online (Sandbox Code Playgroud)

当然,您需要确保考虑一个期望像素值的属性,否则您将拥有一个无效值(如上一个示例中的背景)。


小智 6

一个古怪的方法是添加


:root {
  --example-var: calc(100px - 5px);
}

#var-calculator {
    // can be fetched through .getBoundingClientRect().width
    width: var(--example-var); 

    // rest of these just to make sure this element
    // does not interfere with your page design.
    opacity: 0;
    position: fixed;
    z-index: -1000;
}

Run Code Online (Sandbox Code Playgroud)

 <custom-element>
  <div id="var-calculator"></div>
</custom-element>

Run Code Online (Sandbox Code Playgroud)

const width = document.getElementById('var-calculator').getBoundingClientRect().width

Run Code Online (Sandbox Code Playgroud)

我不知道这是否适用于您的用例,但我只是对其进行了测试并且可以正常工作。