vue 主题更换 elemeny-ui下动态切换主题色

vue 主题更换 elemeny-ui下动态切换主题色
一、文章学习来源
https://www.cnblogs.com/rogerwu/p/9542165.html
二、在这里插入代码片

<template>
  <el-color-picker
    class="theme-picker"
    popper-class="theme-picker-dropdown"
    v-model="theme"></el-color-picker>
</template>
<script>
const version = require('element-ui/package.json').version // element-ui version from node_modules
var ORIGINAL_THEME = '#409EFF' // default color

export default {
  data() {
    return {
      chalk: '', // content of theme-chalk css
      theme: '#296BD1'
    }
  },
  watch: {
    theme(val, oldVal) {
      this.changeColor(val, oldVal).then(()=>{
        this.$message({
          message: '换肤成功',
          type: 'success'
        })
      })
    }
  },
  created(){
    this.theme = '#296BD1'
    this.changeColor('#296BD1', '#296BD1').then(()=>{})
  },
  methods: {
    changeColor(val, oldVal ){
      console.log(val,oldVal)
      return new Promise((resolve,reject)=>{
        if (typeof val !== 'string') return
        const themeCluster = this.getThemeCluster(val.replace('#', ''))
        const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))
        const getHandler = (variable, id) => {
          return () => {
            const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''))
            const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster)
              this.$store.dispatch("getMainColor", val)
            let styleTag = document.getElementById(id)
            if (!styleTag) {
              styleTag = document.createElement('style')
              styleTag.setAttribute('id', id)
              document.head.appendChild(styleTag)
            }
            styleTag.innerText = newStyle
            resolve()
          }
        }
        const chalkHandler = getHandler('chalk', 'chalk-style')
        if (!this.chalk) {
          const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
          
          this.getCSSString(url, chalkHandler, 'chalk')
        } else {
          chalkHandler()
        }
        const styles = [].slice.call(document.querySelectorAll('style'))
          .filter(style => {
            const text = style.innerText
            return new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text)
          })
        styles.forEach(style => {
          const { innerText } = style
          if (typeof innerText !== 'string') return
          style.innerText = this.updateStyle(innerText, originalCluster, themeCluster)
      })
      })
    },
    updateStyle(style, oldCluster, newCluster) {
      let newStyle = style
      oldCluster.forEach((color, index) => {
        newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
      })
      return newStyle
    },
    getCSSString(url, callback, variable) {
      const xhr = new XMLHttpRequest()
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status === 200) {
          this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '')
          callback()
        }
      }
      xhr.open('GET', url)
      xhr.send()
    },
    getThemeCluster(theme) {
      console.log(theme)
      const tintColor = (color, tint) => {
        let red = parseInt(color.slice(0, 2), 16)
        let green = parseInt(color.slice(2, 4), 16)
        let blue = parseInt(color.slice(4, 6), 16)
        if (tint === 0) { // when primary color is in its rgb space
          return [red, green, blue].join(',')
        } else {
          red += Math.round(tint * (255 - red))
          green += Math.round(tint * (255 - green))
          blue += Math.round(tint * (255 - blue))
          red = red.toString(16)
          green = green.toString(16)
          blue = blue.toString(16)
          return `#${red}${green}${blue}`
        }
      }
      const shadeColor = (color, shade) => {
        let red = parseInt(color.slice(0, 2), 16)
        let green = parseInt(color.slice(2, 4), 16)
        let blue = parseInt(color.slice(4, 6), 16)
        red = Math.round((1 - shade) * red)
        green = Math.round((1 - shade) * green)
        blue = Math.round((1 - shade) * blue)
        red = red.toString(16)
        green = green.toString(16)
        blue = blue.toString(16)
        return `#${red}${green}${blue}`
      }
      const clusters = [theme]
      for (let i = 0; i <= 9; i++) {
        clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
      }
      clusters.push(shadeColor(theme, 0.1))
      return clusters
    }
  }
}
</script>
<style>
.theme-picker .el-color-picker__trigger {
  vertical-align: middle;
}

.theme-picker-dropdown .el-color-dropdown__link-btn {
  display: none;
}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Element-UI 是一个基于 Vue.js 的开源UI组件库,可以用于构建Web应用程序的用户界面。要在 Vue 3 中使用 Element-UI,您需要按照以下步骤进行设置: 1. 在项目路径下的终端中运行以下命令来安装 Element-UI:`npm i element-ui -S`。 2. 在 main.js 中通过 import 导入 Element-UI: ```javascript import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); ``` 3. 如果您只想按需引入 Element-UI 中的某些组件,可以按照以下步骤进行设置: a. 通过 import 导入指定的组件,例如 Button: ```javascript import { Button } from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(Button); ``` b. 安装 babel-plugin-component 插件以减小项目的体积:`npm install babel-plugin-component -D`。 c. 在项目根目录下的 babel.config.js 文件中添加以下代码: ```javascript module.exports = { presets: [ '@vue/cli-plugin-babel/preset' ], plugins: [ [ 'component', { 'libraryName': 'element-ui', 'styleLibraryName': 'theme-chalk' } ] ] } ``` 关于Vue 3 的 Element-UI 的更多信息和使用示例,您可以参考以下内容: 1. [Vue Element-UI 后台管理项目实战(项目概述【附源码】)](链接一) 2. [Vue Element-UI 后台管理项目实战(一)](链接二) 3. [Vue Element-UI 后台管理项目实战(二)](链接三) 4. [Vue Element-UI 后台管理项目实战(三)](链接四) 5. [Vue Element-UI 后台管理项目实战(四)](链接五) 6. [Vue Element-UI 后台管理项目实战(五)](链接六) 7. [Vue Element-UI 后台管理项目实战(六)](链接七) 8. [Vue Element-UI 后台管理项目实战(七)](链接八) 9. [Vue Element-UI 后台管理项目实战(八)(完结)](链接九)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值