elementui 更换主题色

定义CSS变量,由var()函数来获取值。

复制代码
/* 设定值 */
:root {
    --background-color: #FF0000;
}
/* 获取值 */
div {
    background-color: var(--background-color);
}

JS获取变量

复制代码
const element = document.documentElement;
const style = window.getComputedStyle(element);
const value = style.getPropertyValue('`background-color`');

JS设置变量

复制代码
el.style.setProperty('background-color', value)

动态修改 ElementUI 主题色

复制代码
changeTheme(color, theme) {
      const version = require('element-ui/package.json').version
      const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
      const id = 'chalk-style'
      return new Promise(resolve => {
        const xhr = new XMLHttpRequest()
        xhr.onreadystatechange = () => {
          if (xhr.readyState === 4 && xhr.status === 200) {
            const res = xhr.responseText
            const newStyle = res.replace(new RegExp(color, 'ig'), theme)
            let styleTag = document.getElementById(id)
            if (!styleTag) {
              styleTag = document.createElement('style')
              styleTag.setAttribute('id', id)
              document.head.appendChild(styleTag)
            }
            styleTag.innerText = newStyle
            resolve()
          }
        }
        xhr.open('GET', url)
        xhr.send()
      })
}

1

相关推荐
We་ct15 分钟前
LeetCode 300. 最长递增子序列:两种解法从入门到优化
开发语言·前端·javascript·算法·leetcode·typescript
深海鱼在掘金16 分钟前
Next.js从入门到实战保姆级教程(第一章):导读——建立 Next.js 的认知框架
前端·typescript·next.js
渔舟小调18 分钟前
P17 | 管理台动态路由:后端返回菜单树,前端运行时注入
前端
小徐_233332 分钟前
uni-app 组件库 Wot UI 2.0 发布了,我们带来了这些改变!
前端·微信小程序·uni-app
❀͜͡傀儡师37 分钟前
Claude Code 官方弃用 npm 安装方式:原因分析与完整迁移指南
前端·npm·node.js·claude code
知识分享小能手43 分钟前
ECharts入门学习教程,从入门到精通,ECharts高级功能(6)
前端·学习·echarts
阿祖zu43 分钟前
OpenClaw 入门到放弃:私人 AI 的初代原型机
前端·后端·aigc
ZC跨境爬虫44 分钟前
3D地球卫星轨道可视化平台开发 Day15(添加卫星系列模糊搜索功能)
前端·数据库·3d·交互·数据可视化
m0_738120721 小时前
渗透基础知识ctfshow——Web应用安全与防护(第六 七章)
服务器·前端·安全
Sun子矜1 小时前
Web项目18+项目21
前端