JavaScript allows access to CSS custom properties (also known as CSS variables)
P粉420868294
P粉420868294 2023-10-20 22:42:04
0
2
685

How do I get and set CSS custom properties (properties accessed via var(…) in a stylesheet) using JavaScript (plain or jQuery)?

Here is my unsuccessful attempt: clicking the button changes the usual font-weight properties, but not the custom --mycolor properties:

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <style>
    body { 
      --mycolor: yellow;
      background-color: var(--mycolor);
    }
  </style>
</head>
<body>

  <p>Let's try to make this text bold and the background red.</p>
  <button onclick="plain_js()">Plain JS</button>
  <button onclick="jQuery_()">jQuery</button>

  <script>
  function plain_js() { 
    document.body.style['font-weight'] = 'bold';
    document.body.style['--mycolor'] = 'red';
  };
  function jQuery_() {
    $('body').css('font-weight', 'bold');
    $('body').css('--mycolor', 'red');
  }
  </script>
</body>
</html>

P粉420868294
P粉420868294

reply all(2)
P粉421119778

Native solution

The standard methods for getting/setting CSS3 variables are .setProperty() and .getPropertyValue().

If your variables are global variables (declared in :root), you can use the following to get and set their values.

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

However, if the value of var has been set using .setProperty(), the getter will only return that value. If set via a CSS declaration, undefined will be returned. Check it out in this example:

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>

To avoid this unexpected behavior, you must use the getCompulatedStyle() method before calling .getPropertyValue(). The getter will then look like this:

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

In my opinion, accessing CSS variables should be simpler, faster, intuitive and natural...


My personal approach

I implemented CSSGlobalVariables a small ( for easier Access and Operation.

// get the document CSS global vars
let cssVar = new CSSGlobalVariables();
// set a new value to --myVariable
cssVar.myVariable = 'red';
// get the value of --myVariable
console.log( cssVar.myVariable );

Any changes applied to object properties are automatically converted to CSS variables.

Available: https://github.com/colxi/ css-global variables

P粉863295057

You can use document.body.style.setProperty('--name', value);:

var bodyStyles = window.getComputedStyle(document.body);
var fooBar = bodyStyles.getPropertyValue('--foo-bar'); //get

document.body.style.setProperty('--foo-bar', newValue);//set
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!