如何使用JS编辑CSS变量?

Dav*_*rds 30 javascript css jquery

我有这些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);
}
Run Code Online (Sandbox Code Playgroud)

但无论我如何尝试更改属性(两个注释行单独尝试),我得到的最接近的不是有效属性.

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");
}
Run Code Online (Sandbox Code Playgroud)

错误消息

Bre*_*ody 28

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

var html = document.getElementsByTagName('html')[0];
html.style.cssText = "--main-background-color: red";
Run Code Online (Sandbox Code Playgroud)

要么

var html = document.getElementsByTagName('html')[0];
html.style.setProperty("--main-background-color", "green");
Run Code Online (Sandbox Code Playgroud)

要么

var html = document.getElementsByTagName('html')[0];
html.setAttribute("style", "--main-background-color: green");
Run Code Online (Sandbox Code Playgroud)

演示

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

window.onload = function() {
  setTimeout(function() {
    var html = document.getElementsByTagName('html')[0];
    html.style.cssText = "--main-background-color: red";
  }, 2000);
};
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)

这只适用于支持CSS变量的浏览器.

  • 这将覆盖现有的内联样式. (4认同)
  • 我猜 `html.style.setProperty("--main-background-color", "green");` 是首选方法。使用 `setAttribute('style', ..)` 将覆盖任何其他内联样式,`style.cssText = ...` 也是如此 (2认同)

phi*_*294 16

如果您正在使用:root:

:root {
    --somevar: black;
}
Run Code Online (Sandbox Code Playgroud)

它将是documentElement.

document.documentElement.style.setProperty('--somevar', 'green');
Run Code Online (Sandbox Code Playgroud)


Ori*_*iol 12

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

document.body.style.setProperty('--background-color', 'blue');
Run Code Online (Sandbox Code Playgroud)
body {
  --background-color: red;
  background-color: var(--background-color);
}
Run Code Online (Sandbox Code Playgroud)


col*_*lxi 10

原生解决方案

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

如果您的变量是Globals(声明为:root),则可以使用以下内容来获取和设置它们的值.

// setter
document.documentElement.style.setProperty('--myVariable', 'blue');
// getter
document.documentElement.style.getPropertyValue('--myVariable');
Run Code Online (Sandbox Code Playgroud)

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

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

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

getComputedStyle(document.documentElement,null).getPropertyValue('--myVariable');
Run Code Online (Sandbox Code Playgroud)

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


我的个人方法

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

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

应用于Object属性的任何更改都会自动转换为CSS变量.

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


Mar*_*ark 6

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

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

.selector:after {
    content: var(--my-css-var);
}    
Run Code Online (Sandbox Code Playgroud)

这不起作用:

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

但这确实:

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