2021-06-28 CSS变量设置颜色

8 篇文章 0 订阅
5 篇文章 0 订阅

说明

  1. css变量只能以[–]开头。
  2. 距离当前dom元素层级最近的父元素作用域css变量的值更容易生效。
  3. css变量在css文件中只能以var()函数来使用。

代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>css变量设置颜色</title>
</head>
<style>
  :root {/* //设置css变量作用域; */
    --theRoot: rgb(255, 255, 0);
    --fangBorder: rgb(255, 0, 0);
  }
  .myDiv {
    --theDiv: rgb(0, 255, 0);
    --fangBorder: rgb(0, 0, 255);
  }
  body {
    background: var(--theRoot);
    border: solid 5px var(--fangBorder);/* //css变量只能以var()函数来使用; */
  }

  .myDiv {
    width: 100px;
    height: 100px;
    background: var(--theDiv);
    border: solid 5px var(--fangBorder);
  }
</style>
<body>
  <div class="myDiv"></div>
</body>
<script>
  /* //不建议使用,和预期不符;虽然操作逻辑更统一;//仿阮一峰的改;
  setTimeout(() => {
    //let rootElement = document.documentElement;
    let rootElement = document.querySelector(":root");
    let rootStyles = rootElement.style;
    let rootStyleValue = rootStyles.getPropertyValue("--fangBorder");//只能读取通过js设置后的css变量;
    console.log("代码设置前: rootStyleValue--->", rootStyleValue);
    rootStyles.setProperty("--fangBorder", "rgb(255, 255, 255)");
    rootStyleValue = rootStyles.getPropertyValue("--fangBorder");
    console.log("代码设置后: rootStyleValue--->", rootStyleValue);

    let thisElement = document.querySelectorAll(".myDiv")[0];
    let thisStyles = thisElement.style;
    let thisStyleValue = thisStyles.getPropertyValue("--fangBorder"); // 读取变量;
    console.log("代码设置前: thisStyleValue--->", thisStyleValue);
    thisStyles.setProperty("--fangBorder", "#7F583F"); // 设置变量;
    thisStyleValue = thisStyles.getPropertyValue("--fangBorder");
    console.log("代码设置后: thisStyleValue--->", thisStyleValue);

    //thisStyles.removeProperty("--fangBorder"); // 删除变量;删除后,表现上会变成浏览器的默认值;//但此时使用getComputedStyle()来计算,依旧是自己在css里写的值;与预期不符,不建议使用这个方法来移除css文件里书写的css变量;
  }, 6000); */

  //建议实际场景里使用,与预期大致一样;
  setTimeout(() => {
    let rootElement = document.querySelector(":root");
    let rootStyles = getComputedStyle(rootElement);
    let rootStyleValue = rootStyles.getPropertyValue("--fangBorder");
    console.log("代码设置前: rootStyleValue--->", rootStyleValue);
    rootElement.style.setProperty("--fangBorder", "rgb(0, 0, 0)");
    rootStyleValue = rootStyles.getPropertyValue("--fangBorder");
    console.log("代码设置后: rootStyleValue--->", rootStyleValue);

    let thisElement = document.querySelectorAll(".myDiv")[0];
    let thisStyles = getComputedStyle(thisElement); // 计算并获取元素的样式表;计算出来的样式表只能只读;
    let thisStyleValue = thisStyles.getPropertyValue("--fangBorder"); // 计算并获取CSS变量;
    console.log("代码设置前: thisStyleValue--->", thisStyleValue);
    thisElement.style.setProperty("--fangBorder", "rgb(255, 255, 255)"); //设置CSS变量;
    thisStyleValue = thisStyles.getPropertyValue("--fangBorder");
    console.log("代码设置后: thisStyleValue--->", thisStyleValue);
  }, 3000);
</script>
</html>

来源:

  1. CSS 变量教程;
  2. CSS变量(自定义属性)实践指南;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值