«

vue-element怎么实现自主换肤配置

时间:2024-5-26 10:53     作者:韩俊     分类: Javascript


今天小编给大家分享一下vue-element怎么实现自主换肤配置的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

element换肤所有主题色和基础色均可自主配置

1.element-ui官方提供的动态切换主题方法换肤 但此方法只可修改$–color-primary 这一个主题色及其衍生色

2. 获取element-ui的theme-chalk/index.css文件,找到基础色所对应的颜色值,抽取颜色值进行替换,此种方法无法自动生成衍生色

 {
        '#67C23A': theme.color_success,
        '#E6A23C': theme.color_warning,
        '#F56C6C': theme.color_danger,
        '#909399': theme.color_info,
    }

3.自定义css变量,使用下面的方法去修改变量,此种方法仅可修改自定义的样式

document.documentElement.style.setProperty('--theme-color', value)

最终方案

以上三种相结合,实现我们的需求,各种主题色、基础色自主配置

以下是所有代码

Theme.vue

···

ht-form-item
是自定义组件 和
el-form-item
差不多

<template>
  <div>
    <h5 class="header">
      <span>主题配色选择</span>
      <el-button type="default" @click="resetTheme">重置</el-button>
    </h5>
    <el-row>
      <el-col :span="8">
        <ht-form-item label="主题颜色" label-width="140px">
          <el-color-picker
            v-model="theme"
            :predefine="['#409eff', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="成功颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_success"
            :predefine="['#67c23a', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="警告颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_warning"
            :predefine="['#e6a23c', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="危险颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_danger"
            :predefine="['#f56c6c', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="信息颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_info"
            :predefine="['#909399', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="次要主题颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_secondary"
            :predefine="['#dfebfc', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
      </el-col>
      <el-col :span="8">
        <ht-form-item label="基础白色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_white"
            :predefine="['#fff', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="基础黑色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_black"
            :predefine="['#000', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="主要文字颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_text_primary"
            :predefine="['#303133', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="常规文字颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_text_regular"
            :predefine="['#606266', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="次要文字颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_text_secondary"
            :predefine="['#909399', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="占位文字颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.color_text_placeholder"
            :predefine="['#c0c4cc', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
      </el-col>
      <el-col :span="8">
        <ht-form-item label="一级边框颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.border_color_base"
            :predefine="['#dcdfe6', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="二级边框颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.border_color_light"
            :predefine="['#e4e7ed', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="三级边框颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.border_color_lighter"
            :predefine="['#ebeef5', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="四级边框颜色" label-width="140px">
          <el-color-picker
            v-model="themeColor.border_color_extra_light"
            :predefine="['#f2f6fc', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="基础背景色" label-width="140px">
          <el-color-picker
            v-model="themeColor.background_color_base"
            :predefine="['#f5f7fa', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
        <ht-form-item label="第二基础背景色" label-width="140px">
          <el-color-picker
            v-model="themeColor.background_color_secondary"
            :predefine="['#f0f2f5', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="theme-picker"
            popper-class="theme-picker-dropdown"
          />
        </ht-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="8">
        <ht-form-item label="首页导航颜色" label-width="140px">
          <span class="color-box">
            <span class="color-block" :></span>
          </span>
        </ht-form-item>
      </el-col>
      <el-col :span="8">
        <ht-form-item label="首页导航悬浮颜色" label-width="140px">
          <span class="color-box">
            <span class="color-block" :></span>
          </span>
        </ht-form-item>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {mapState} from 'vuex';
import variables from '@/assets/css/theme-variables.scss'
const version = require('element-ui/package.json').version
const ORIGINAL_THEME = '#409EFF' // default color

export default {
  data() {
    return {
      themeColor: {},
      chalk: '', // content of theme-chalk css
      theme: ORIGINAL_THEME,
      theme_color: '#0d84ff',
      theme_color_active: '#006bd9',
    }
  },
  computed: {
    ...mapState({
      themeVaribles: state => state.theme,
      defaultTheme: state => state.theme.color_primary
    })
  },
  watch: {
    defaultTheme: {
      handler: function(val, oldVal) {
        this.theme = val
      },
      immediate: true
    },
    themeColor: {
      handler: function (theme, oldTheme) {
        // element默认主题配色值
        this.setIndexStyle(this.getColorMap(theme))
        this.setCssVariables(theme)        
        // vuex存储颜色
        for(let x in theme) {
          this.$store.dispatch('theme/changeSetting', {
            key: x,
            value: theme[x]
          })
        }
        localStorage.setItem('themeColor',JSON.stringify(theme))
      },
      deep: true
    },
    theme(val,oldVal) {
      if (typeof val !== 'string') return
      const themeCluster = this.getThemeCluster(val.replace('#', ''))
      const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))
      // console.log(themeCluster, originalCluster)

      const getHandler = (variable, id) => {
        return () => {
          const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''))
          let newStyle = this.updateStyle(this[variable], originalCluster, themeCluster)
          // 将其他配色添加进去
          newStyle = this.getStyleTemplate(newStyle,this.getColorMap(this.themeColor));

          let styleTag = document.getElementById(id)
          styleTag.innerText = newStyle
        }
      }

      const chalkHandler = getHandler('chalk', 'my-chalk')
      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)
      })
      //this.themeColor.color_primary = val
      localStorage.setItem('color_primary',val)
      this.$store.dispatch('theme/changeSetting', {
        key: 'color_primary',
        value: val
      })

      this.theme_color = this.generaterSimilarColor(val,0.1)
      this.theme_color_active = this.generaterSimilarColor(val,0.2)
      const node = document.documentElement
      node.style.setProperty('--theme-color', this.theme_color)
      node.style.setProperty('--theme-color-active', this.theme_color_active)
    }
  },
  created() {
    // 获取index.css
    this.getFile(`//unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`)
    .then(({ data }) => {
      data = data.replace(/@font-face{[^}]+}/, '')
      this.chalk = data
      const style = document.createElement('style');
      style.id = 'my-chalk'
      style.innerText = this.chalk;
      document.head.appendChild(style);

      // 获取本地存储的基础色
      if(localStorage.getItem('themeColor')) {
        this.themeColor = JSON.parse(localStorage.getItem('themeColor'))
        for(let x in this.themeColor) {
          this.$store.dispatch('theme/changeSetting', {
            key: x,
            value: this.themeColor[x]
          })
        }
      } else {
        this.themeColor = Object.assign({},this.themeVaribles)
      }
      // 获取本地存储的主题色
      if(localStorage.getItem("theme")) {
        this.theme = localStorage.getItem("theme")
        this.$store.dispatch('theme/changeSetting', {
          key: 'color_primary',
          value: this.theme
        })
      }
    });
  },
  methods: {
    // 重置主题
    resetTheme() {
      this.theme = ORIGINAL_THEME
      let aVariables = {...variables}
      delete aVariables.color_primary
      this.themeColor = {...aVariables}
    },
    getColorMap(theme) {
      return {
       // '#409EFF': theme.color_primary,
        '#67C23A': theme.color_success,
        '#E6A23C': theme.color_warning,
        '#F56C6C': theme.color_danger,
        '#909399': theme.color_info,
        '#dfebfc': theme.color_secondary,
        '#ffffff': theme.color_white,
        '#000000': theme.color_black,
        '#303133': theme.color_text_primary,
        '#606266': theme.color_text_regular,
        '#909399': theme.color_text_secondary,
        '#C0C4CC': theme.color_text_placeholder,
        '#DCDFE6': theme.border_color_base,
        '#E4E7ED': theme.border_color_light,
        '#EBEEF5': theme.border_color_lighter,
        '#F2F6FC': theme.border_color_extra_light,
        '#F5F7FA': theme.background_color_base,
        '#F0F2F5': theme.background_color_secondary
      }
    },
    // css变量改变赋值
    setCssVariables(theme) {
      const node = document.documentElement
      Object.entries(theme).forEach(ele => {
        let key = ele[0].replace(/_/g, '-');
        node.style.setProperty(`--${key}`, ele[1])
      })
    },
    // 颜色替换后的样式文件
    getStyleTemplate(data,colorMap) {
      Object.keys(colorMap).forEach(key => {
        const value = colorMap[key];
        data = data.replace(new RegExp(key, 'ig'), value);
      });
      return data;
    },
    // 获取文件
    getFile(url, isBlob = false) {
      return new Promise((resolve, reject) => {
        const client = new XMLHttpRequest();
        client.responseType = isBlob ? 'blob' : '';
        client.onreadystatechange = () => {
          if (client.readyState !== 4) {
            return;
          }
          if (client.status === 200) {
            const urlArr = client.responseURL.split('/');
            resolve({
              data: client.response,
              url: urlArr[urlArr.length - 1]
            });
          } else {
            reject(new Error(client.statusText));
          }
        };
        client.open('GET', url);
        client.send();
      });
    },
    // 修改颜色后替换index.css文件
    setIndexStyle(colorMap) {
      const myChalkStyle = document.getElementById("my-chalk")
      const themeCluster = this.getThemeCluster(this.theme.replace('#', ''))
      const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''))
      let newStyle = this.updateStyle(this.chalk, originalCluster, themeCluster)
      newStyle = this.getStyleTemplate(newStyle,colorMap)
      myChalkStyle.innerText = newStyle;
    },
    updateStyle(style, oldCluster, newCluster) {
      let newStyle = style
      oldCluster.forEach((color, index) => {
        newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
      })
      return newStyle
    },
    // 获取主题色和衍生色
    getThemeCluster(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
    },
    // 生成fvue相近主题色:这是一个十六进制转rgb,rgb再转hsl,hsl改变亮度,再转成rgb,再转成十六进制的方法。。。
    generaterSimilarColor(color,weight) {
     let colorChange = [];
      for (let i=1; i<7; i+=2) {
        colorChange.push(parseInt("0x"+color.slice(i, i+2)));    
      }
      let [r,g,b] = colorChange
      r /= 255, g /= 255, b /= 255;
      let max = Math.max(r, g, b), min = Math.min(r, g, b);
      let h, s, l = (max + min) / 2;
      if (max == min){ 
        h = s = 0; // achromatic
      } else {
        let d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        switch(max) {
          case r: h = (g - b) / d + (g < b ? 6 : 0); break;
          case g: h = (b - r) / d + 2; break;
          case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
      }
      l = l-weight < 0 ? 0.1 : l-weight

      if(s == 0) {
        r = g = b = l; // achromatic
 &a

标签: javascript vue

热门推荐