zoukankan      html  css  js  c++  java
  • 关于使用css变量实现主题的切换效果

    现在要实现网页主题的切换成本较小的一种方案就是使用css的变量来实现

    HTML

    在HTML的body标签上先定义一个id元素属性

    <body id="sm-theme">
    	<sj-root></sj-root>
    </body>
    

    CSS

    css定义css变量,变量的定义必须要以 -- 开头

    #sm-theme {
    	--smTheme: #2A2A2A;
    	--smSettingRight: #484848;
    	--smSettingRightBox: #2A2A2A;
    }
    

    js

    在任何地方使用js/ts来获取body上定义的sm-theme属性名从而操作定义在里面的css样式变量

     changeTheme(colorName) {
        const theamEle = document.getElementById('sm-theme'); // 获取id为sm-theme的元素
        theamEle.style.setProperty('--smTheme', colorName); // 设置--smTheme变量为我们想要的颜色
        const theamColor = getComputedStyle(theamEle).getPropertyValue('--smTheme'); // 获取--smTheme变量的颜色
        if (theamColor === '#2A2A2A') {
          theamEle.style.setProperty('--smSettingRight', '#484848');
          theamEle.style.setProperty('--smSettingRightBox', '#2A2A2A');
        } else {
          theamEle.style.setProperty('--smSettingRight', 'rgba(180,180,180,0.4)');
          theamEle.style.setProperty('--smSettingRightBox', '#5C5C5C');
        }
    
  • 相关阅读:
    代码希望HTML5初探CSS3新特性小示例
    myeclipse及eclipse的优化
    window7如何提高到最高权限
    大麦茶
    poj3292
    poj3278
    poj3100
    poj3117
    poj3299
    Presto性能调优的五大技巧
  • 原文地址:https://www.cnblogs.com/yuanchao-blog/p/12207000.html
Copyright © 2011-2022 走看看