小编Ari*_*reu的帖子

CSS变量-交换值?

我有一个非常简单的CSS变量问题。我想交换两个CSS变量,基本上等同[a, b] = [b, a]于ES6中的CSS变量。这是一个简单的例子:

<p>White background</p>
<button>Black background</button>
<div>
  <p>Black background</p>
  <button>White background</button>
</div>
Run Code Online (Sandbox Code Playgroud)
:root {
  --primary-color: #fff;
  --secondary-color: #000;
}

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

button {
  background-color: var(--secondary-color);
}

div {
  /* i'd like to do the following: */
  --primary-color: var(--secondary-color);
  --secondary-color: var(--primary-color);

  /* so here, `--primary-color` would be `--secondary-color` from `:root`
   * and any children have these colors swapped as well
   */
  background-color: var(--primary-color);
}
Run Code Online (Sandbox Code Playgroud)

但是,这失败了,因为CSS var()是实时绑定。我在这里想念什么吗?还是这是规范当前的工作方式?

css swap css-variables

4
推荐指数
1
解决办法
97
查看次数

标签 统计

css ×1

css-variables ×1

swap ×1