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

相关推荐
小屁孩大帅-杨一凡33 分钟前
一个简单点的js的h5页面实现地铁快跑的小游戏
开发语言·前端·javascript·css·html
读心悦37 分钟前
CSS 布局系统深度解析:从传统到现代的布局方案
前端·css
椒盐螺丝钉43 分钟前
CSS盒子模型:Padding与Margin的适用场景与注意事项
前端·css
萧鼎2 小时前
构建全栈 Web 应用的新选择:NextPy 技术详解与实战指南
前端
purpleseashell_Lili2 小时前
配置别名路径 @
javascript·react
这个一个非常哈2 小时前
VUE篇之自定义组件使用v-model
前端·javascript·vue.js
purpleseashell_Lili2 小时前
react 基本写法
java·服务器·前端
哎哟喂_!2 小时前
Node.js 循环依赖问题详解:原理、案例与解决方案
前端·chrome·node.js
热爱前端的小君同学3 小时前
长按拖拽移动的vue3组件
前端·javascript·vue.js
Rhys..3 小时前
如何禁止chrome自动更新
前端·chrome