JavaScript 检测浏览器是否支持 CSS 变量:

const isSupported =
  window.CSS &&
  window.CSS.supports &&
  window.CSS.supports('--a', 0);

if (isSupported) {
  /* supported */
} else {
  /* not supported */
}

 

JavaScript 操作 CSS 变量的写法如下:

// 设置变量
document.body.style.setProperty('--primary', '#7F583F');

// 读取变量
document.body.style.getPropertyValue('--primary').trim();
// '#7F583F'

// 删除变量
document.body.style.removeProperty('--primary');


原文:http://www.ruanyifeng.com/blog/2017/05/css-variables.html
posted on 2020-04-02 14:57  Qionghuihe  阅读(337)  评论(0编辑  收藏  举报