js动态设置css主题(Style-setProperty)

hex颜色转RGB


javascript 复制代码
hex2rgb(str = '') {
        str = str.replace("#", "");
        const hxs: any = str.match(/../g);
        for (let index = 0; index < 3; index++) hxs[index] = parseInt(hxs[index], 16);
        return hxs;
    }

RGB转HXS


javascript 复制代码
rgb2hex(r: any, g: any, b: any) {
        const hexs = [r.toString(16), g.toString(16), b.toString(16)];
        for (let index = 0; index < 3; index++) {
            const isHave = hexs[index].length == 1;
            if (isHave) hexs[index] = "0" + hexs[index];
        }
        return "#" + hexs.join("");
    }

颜色加深


javascript 复制代码
getDark(color: any, level: any) {
        const rgb = this.hex2rgb(color);
        for (let index = 0; index < 3; index++) {
            rgb[index] = Math.floor(rgb[index] * (1 - level))
        }
        return this.rgb2hex(rgb[0], rgb[1], rgb[2]);
    }

颜色变淡


javascript 复制代码
getLight(color: any, level: any) {
        const rgb = this.hex2rgb(color);
        for (let index = 0; index < 3; index++) {
            rgb[index] = Math.floor((255 - rgb[index]) * level + rgb[index])
        }
        return this.rgb2hex(rgb[0], rgb[1], rgb[2]);
    }

定义后端返回主题色


定义的参考色,在开发的过程中希望后端人员能遵循

javascript 复制代码
const themeConf={
    primary: '#183ee4',
	success: '#0cce63',
	warn: '#ff4900',
	danger: '#f00c63'
}
javascript 复制代码
getTheme(temeConf) {
    const obj={};
    Object.keys(temeConf).forEach(key => {
	    let color = temeConf[key];
        // 用于按钮点击颜色
	    const dColor = this.getDark(color, 0.2);
	    obj[`--${key}`] = color;
	    obj[`--${key}--active`] = dColor;
	    for (let num = 1; num <= 4; num++) {
	        const val = this.getLightColor(color, num / 10);
	        obj[`--${key}-${num}`] = val;
	    }
    });
    return obj;
}

将组元录入

1、在index.html内添加style标签录入(可录入::root下不挂在到任何标签)

javascript 复制代码
setTheme(temeConf: any) {
        let eStr: string = '';
        Object.keys(temeConf).forEach(key => {
            let color = temeConf[key];
            // 用于按钮点击颜色
            const dColor = this.getDark(color, 0.2);
            eStr += `--${key}:${color};`;
            eStr += `--${key}--active:${dColor};`;
            for (let num = 1; num <= 4; num++) {
                const val = this.getLight(color, num / 10);
                eStr += `--${key}-${num}:${val};`;
            }
        });
        let styleDom = document.getElementById('#sysCssElemnet');
        if (!styleDom) {
            styleDom = document.createElement("style");
            styleDom.id = "#sysCssElemnet";
            document.head.append(styleDom);
        }
        styleDom.innerHTML = `:root{${eStr}}`;
    }

2、将组元录入到html

javascript 复制代码
let html=document.documentElement;
const themeObj=getTheme(themConf);
Object.keys(themeObj).forEach(key=>html.style.setProperty(key,colorObj[key]));

注:如果采用第二种方式录入,建议可直接在getTheme时直接在for循环中setProperty相关组元属性

相关推荐
__ocean几秒前
编译Qt5.15.16并启用pdf模块
开发语言·qt·pdf
万物得其道者成2 分钟前
从零开始创建一个 Next.js 项目并实现一个 TodoList 示例
开发语言·javascript·ecmascript
海天胜景9 分钟前
无法加载文件 E:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本
前端·npm·node.js
MingT 明天你好!11 分钟前
在vs code 中无法运行npm并报无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查
前端·npm·node.js·visual studio code
老兵发新帖17 分钟前
pnpm 与 npm 的核心区别
前端·npm·node.js
超级土豆粉18 分钟前
怎么打包发布到npm?——从零到一的详细指南
前端·npm·node.js
OpenTiny社区23 分钟前
TinyEngine 2.5版本正式发布:多选交互优化升级,页面预览支持热更新,性能持续跃升!
前端·低代码·开源·交互·opentiny
77tian24 分钟前
设计模式的原理及深入解析
java·开发语言·单例模式·设计模式·代理模式·享元模式·原型模式
幽蓝计划26 分钟前
仓颉开发语言入门教程:搭建开发环境
开发语言·鸿蒙
Dovis(誓平步青云)41 分钟前
探索C++面向对象:从抽象到实体的元规则(上篇)
开发语言·c++·经验分享·笔记·学习方法