如何使用 JS 编辑 CSS 变量?

IT技术 javascript jquery css
2021-01-19 14:59:37

我有这些 CSS 变量来控制我的项目的颜色,所以我可以做主题。

html {
    --main-background-image: url(../images/starsBackground.jpg);
    --main-text-color: #4CAF50;
    --main-background-color: rgba(0,0,0,.25);
    --beta-background-color: rgba(0,0,0,.85);
}

但是,无论我如何尝试更改属性(单独尝试的两条注释行),我得到的最接近的是返回无效属性。

function loadTheme() {
    var htmlTag = document.getElementsByTagName("html");
    var yourSelect = document.getElementById( "themeSelect" );
    var selectedTheme = ( yourSelect.options[ yourSelect.selectedIndex ].value );
    // htmlTag[0].setAttribute('--main-text-color', '#FFCF40');
    // $("html").css("--main-text-color","#FFCF40");
}

错误信息

6个回答

事实证明,可以使用el.style.cssText属性el.style.setPropertyel.setAttribute方法来更改 CSS 变量在您的代码片段el.setAttribute中使用不正确,这导致了您遇到的错误。这是正确的方法:

document.documentElement.style.cssText = "--main-background-color: red";

或者

document.documentElement.style.setProperty("--main-background-color", "green");

或者

document.documentElement.setAttribute("style", "--main-background-color: green");

演示

以下演示使用 CSS 变量定义背景颜色,然后在加载 2 秒后使用 JS 代码段更改它。

window.onload = function() {
  setTimeout(function() {
    document.documentElement.style.cssText = "--main-background-color: red";
  }, 2000);
};
html {
    --main-background-image: url(../images/starsBackground.jpg);
    --main-text-color: #4CAF50;
    --main-background-color: rgba(0,0,0,.25);
    --beta-background-color: rgba(0,0,0,.85);
}

body {
  background-color: var(--main-background-color);
}

这显然只适用于支持 CSS 变量的浏览器。

是的,你需要像这样修改道具: document.querySelector("html").style.setProperty('width', fixed.width + 'px')但是我无法让它工作
2021-03-26 14:59:37
我猜这html.style.setProperty("--main-background-color", "green");是做这件事的首选方式。使用setAttribute('style', ..)将覆盖任何其他内联样式,同样适用style.cssText = ...
2021-03-26 14:59:37
这将覆盖现有的内联样式。
2021-04-09 14:59:37

如果您正在使用:root

:root {
    --somevar: black;
}

它将是 documentElement。

document.documentElement.style.setProperty('--somevar', 'green');

原生解决方案

获取/设置 CSS3 变量标准方法.setProperty().getPropertyValue()

如果您的变量是全局变量(在 中声明:root),您可以使用以下内容来获取和设置它们的值。

// setter
document.documentElement.style.setProperty('--myVariable', 'blue');
// getter
document.documentElement.style.getPropertyValue('--myVariable');

但是,如果已设置,getter 将仅返回 var 的值,使用.setProperty(). 如果已通过 CSS 声明设置,则返回undefined. 在这个例子中检查它:

let c = document.documentElement.style.getPropertyValue('--myVariable');
alert('The value of --myVariable is : ' + (c?c:'undefined'));
:root{ --myVariable : red; }
div{ background-color: var(--myVariable); }
  <div>Red background set by --myVariable</div>

为了避免这种意外行为,您必须getComputedStyle()在调用之前使用该方法.getPropertyValue()然后吸气剂看起来像这样:

getComputedStyle(document.documentElement,null).getPropertyValue('--myVariable');

在我看来,访问 CSS 变量应该更简单、快速、直观和自然……


我的个人方法

我已经实现CSSGlobalVariables了一个很小的 ​​(<3kb) javascript module,它自动检测并打包到一个对象中,文档中的所有活动 CSS 全局变量,以便于访问和操作

import {CSSGlobalVariables} from './css-global-variables.js';
let cssVar = new CSSGlobalVariables();
// set the CSS global --myColor value to "green"
cssVar.myColor = "green";

应用于对象属性的任何更改都会自动转换为 CSS 变量,反之亦然。

可在https : //github.com/colxi/css-global-variables

您可以简单地使用设置任意 CSS 属性的标准方式: setProperty

document.body.style.setProperty('--background-color', 'blue');
body {
  --background-color: red;
  background-color: var(--background-color);
}

出于某种原因,当 var 用于像这样的转换时,我无法让它工作: left: calc(10% - (var(--width) / 2));
2021-04-06 14:59:37

对于任何为此苦苦挣扎的人,如果您的 CSS 变量是一个句子,您需要将它包装在 qoutes 中。

:root {
  --my-css-var: 'Hello Person!';
}

.selector:after {
    content: var(--my-css-var);
}    

这不起作用:

let myVar = 'Hi Person! (doesnt work)';
document.getElementsByTagName('html')[0].style.setProperty('--my-css-var', myVar);

但这确实:

let myVar = 'Hi Person! (works)';
document.getElementsByTagName('html')[0].style.setProperty('--my-css-var', '"' + myVar + '"');