vue+element ui 主题颜色切换

57 篇文章 0 订阅
本文档展示了如何在Vue项目中结合ElementUI实现主题颜色的动态切换。通过自定义scss变量,使用element-theme工具和sass-loader,实现实时修改主题颜色并更新全局样式。同时,介绍了在main.js中引入自定义主题的方法,并提供了 vuex 状态管理来保存用户选择的主题。
摘要由CSDN通过智能技术生成

vue+element ui 主题颜色切换

效果图
码云地址:

https://gitee.com/zhuxueling/ele-ui-theme.git

在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述

项目结构

在这里插入图片描述

安装elementUI及sass-loader,node-sass

//安装elementui
npm i element-ui -S
//安装sass
npm i sass-loader node-sass -D

安装elementui的自定义主题工具

//首先安装主题工具
npm i element-theme -g
//然后安装chalk主题
npm i element-theme-chalk -D

element-variables.scss文件

/* 改变主题色变量 */
$--color-primary: #1890FF;

/* 改变 icon 字体路径变量,必需 */
$--font-path: '~element-ui/lib/theme-chalk/fonts';

@import "~element-ui/packages/theme-chalk/src/index";

:export {
    theme: $--color-primary;
}  

ThemePicker.vue文件

<template>
  <el-color-picker
    v-model="theme"
    :predefine="['#F5222D', '#FA541C', '#FADB14','#3EAF7C','#13C2C2', '#1890FF', '#722ED1', '#EB2F96']"
    class="theme-picker"
    popper-class="theme-picker-dropdown"
    @change="changeHanlder"
  />
</template>

<script>
  import { mapGetters, mapMutations } from 'vuex';
const version = require('element-ui/package.json').version // element-ui version from node_modules
const ORIGINAL_THEME = '#409EFF' // default color

export default {
  data() {
    return {
      chalk: '', // content of theme-chalk css
      theme: sessionStorage.getItem('them')?sessionStorage.getItem('them'):'#1890FF'
    }
  },
  watch: {
    theme: {
      handler: function(val, oldVal) {
        console.log(val,'监听');
        this.changeHanlder(val)
        this.themeMethod(val)
          console.log(this.defaultTheme,'默认主题');
      },
      immediate: true
    },
  
  },

  methods: {
    changeHanlder(value){
      this.theme = value 
      sessionStorage.setItem('them',value)
    },
     async themeMethod(val) {
      const oldVal = this.chalk ? this.theme : ORIGINAL_THEME
      if (typeof val !== 'string') return
      const themeCluster = this.getThemeCluster(val.replace('#', ''))
      console.log(themeCluster,'kkkkk');
      const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))

      const $message = this.$message({
        message: '  Compiling the theme',
        customClass: 'theme-message',
        type: 'success',
        duration: 0,
        iconClass: 'el-icon-loading'
      })

      const getHandler = (variable, id) => {
        console.log(variable,'variable');
        return () => {
          const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''))
          const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster)

          let styleTag = document.getElementById(id)
          if (!styleTag) {
            styleTag = document.createElement('style')
            styleTag.setAttribute('id', id)
            document.head.appendChild(styleTag)
          }
          styleTag.innerText = newStyle
        }
      }
      //如果不能联网需要请求本地的theme.css文件,并且放在public文件夹下面
      //theme.css文件为https://unpkg.com/element-ui@版本号/lib/theme-chalk/index.css内容

      if (!this.chalk) {
      //const url='theme.css'
        const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
        await this.getCSSString(url, 'chalk')
      }

      const chalkHandler = getHandler('chalk', 'chalk-style')

      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.$emit('change', val)

      $message.close()
    },
    updateStyle(style, oldCluster, newCluster) {
      let newStyle = style
      oldCluster.forEach((color, index) => {
        newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
      })
      return newStyle
    },

    getCSSString(url, variable) {
      return new Promise(resolve => {
        const xhr = new XMLHttpRequest()
        xhr.onreadystatechange = () => {
          if (xhr.readyState === 4 && xhr.status === 200) {
            this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '')
            resolve()
          }
        }
        xhr.open('GET', url)
        xhr.send()
      })
    },

    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
    }
  }
}
</script>

<style>
.theme-message,
.theme-picker-dropdown {
  z-index: 99999 !important;
}

.theme-picker .el-color-picker__trigger {
  height: 26px !important;
  width: 26px !important;
  padding: 2px;
}

.theme-picker-dropdown .el-color-dropdown__link-btn {
  display: none;
}
</style>

store.js文件

import Vue from 'vue'
import Vuex from 'vuex'

import variables from '@/assets/element-variables.scss'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    theme:variables.theme
    // theme: variables.theme
  },
  mutations: {
    CHANGE_SETTING: (state, { key, value }) => {
      // console.log(state, { key, value });
      if (state.hasOwnProperty(key)) {
        state[key] = value
      }
    }

  },
  actions: {
    changeSetting({ commit }, data) {
      commit('CHANGE_SETTING', data)
    }


  }
})

main.js文件

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
// import 'element-ui/lib/theme-chalk/index.css';
import '@/assets/element-variables.scss'

Vue.use(ElementUI);
Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

其他人项目:https://github.com/WangMaoling/Vue-element-UI-theme.git

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以通过以下步骤实现 Vue + Element UI + SCSS 切换颜色的功能: 1. 在 Vue 的 `data` 中定义一个变量 `color` 来存储当前的颜色值,以及定义一个数组 `colors` 用来存储可选的颜色值。 ```javascript data() { return { color: '#409EFF', colors: ['#409EFF', '#67C23A', '#E6A23C', '#F56C6C'] } } ``` 2. 在模板中使用 Element UI 的 `el-color-picker` 组件来展示当前颜色,并绑定 `color` 变量。 ```html <el-color-picker v-model="color"></el-color-picker> ``` 3. 使用 SCSS 定义不同颜色的样式,并使用 Vue 的计算属性来动态生成样式。 ```scss $primary-color: #409EFF; $success-color: #67C23A; $warning-color: #E6A23C; $error-color: #F56C6C; // 定义主题颜色 @mixin theme-color($color) { --color-primary: #{$color}; --color-success: lighten(#{$color}, 15%); --color-warning: lighten(#{$color}, 30%); --color-error: lighten(#{$color}, 45%); } // 默认主题颜色 :root { @include theme-color($primary-color); } // 其他主题颜色 .theme-color { @each $color in $colors { &[data-color="#{$color}"] { @include theme-color($color); } } } ``` 在上面的代码中,使用了 SCSS 的 `@mixin` 和 `@each` 来定义不同颜色的样式,其中 `--color-primary`、`--color-success`、`--color-warning` 和 `--color-error` 是 Element UI 中预定义的 CSS 变量,用于控制不同组件的颜色。 还定义了一个 `theme-color` 类,用来动态生成主题颜色,并使用 `&[data-color="#{$color}"]` 来指定不同颜色对应的样式。 4. 在模板中使用计算属性来动态生成样式类名,并将其绑定到根元素上。 ```html <div :class="'theme-color ' + color" data-color="color"></div> ``` 在上面的代码中,使用了 `:class` 绑定了一个计算属性,其中 `theme-color` 是样式类名前缀,用于指定主题颜色,`color` 是当前选择的颜色值。 至此,一个简单的 Vue + Element UI + SCSS 切换颜色的功能就实现了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值