简体   繁体   English

听 CSS 变量变化?

[英]Listen to CSS variable change?

Is it possible to use a callback to listen to CSS variable changes?是否可以使用回调来监听 CSS 变量的变化? something like this:像这样:

documentElement.addListener('css-var-main-background-color', () => {
  console.log('Value changed');
});

Simple answer: 'No, it's not possible.'简单的回答:“不,这是不可能的。”

But by using the Window.getComputedStyle() method and checking its return value in an interval (bad for performance) you can watch for and react to style changes.但是通过使用 Window.getComputedStyle() 方法并在一个时间间隔内检查其返回值(对性能不利),您可以观察样式更改并对其做出反应。

You can read up on getComputedStyle() here https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle你可以在这里阅读getComputedStyle() https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

Variables defined in style attribute style 属性中定义的变量

If you have full control over the code and can set CSS variables via the style attribute on a DOM element rather than using a stylesheet, you can use MutationObserver to detect changes on that element's style attribute and thus detect changes to the variable.如果您可以完全控制代码并且可以通过 DOM 元素上的style属性而不是使用样式表设置 CSS 变量,则可以使用MutationObserver来检测该元素的 style 属性的变化,从而检测变量的变化。

The variables must be set like this:变量必须像这样设置:

document.documentElement.style.setProperty('--var1', '1');

and NOT like this:而不是这样:

:root {
--var1: 1;
}

Then you can monitor changes to the style attribute of the target element.然后,您可以监视目标元素的样式属性的更改。

let value = '1';

const styleObserver = new MutationObserver((mutations) => {
  const currentValue = mutations[0].target.style.getPropertyValue('--var1');

  if (currentValue !== value) {
    // the variable has changed
    value = currentValue;
  }
});

styleObserver.observe(document.documentElement, {
  attributes: true,
  attributeFilter: ['style'],
});

The above example is for the html element which is usually where you put global variables, but it can work with any other element.上面的示例适用于通常放置全局变量的 html 元素,但它可以与任何其他元素一起使用。

Variables defined in a stylesheet样式表中定义的变量

The most universal solution is to use setInterval and getComputedStyle like the other answer suggests, but this is not something you would like to have in a production environment.最通用的解决方案是像其他答案建议的那样使用setIntervalgetComputedStyle ,但这不是您希望在生产环境中拥有的东西。

There is a possible workaround if you are willing to define the stylesheet in a specific way:如果您愿意以特定方式定义样式表,有一种可能的解决方法:

  • Variables must be defined in a <style></style> element变量必须在<style></style>元素中定义
  • The style element should contain only the variables you want to watch to keep it as specific as possible and not trigger the observer when unrelated properties change style 元素应该只包含你想要观察的变量,以尽可能保持它的具体性,并且在不相关的属性发生变化时不会触发观察者

Then you can use MutationObserver to watch changes to the style element and parse its contents to detect which variables have changed.然后您可以使用MutationObserver来观察样式元素的变化并解析其内容以检测哪些变量发生了变化。

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

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