赞
踩
win10 64
v16.13.2
^3.2.33
^2.2.0
themes.js
文件放到项目中。 themes.js
(当然你的文件名字如果不是这个请根据实际情况来) 主题文件。src -> utils -> themes.js
目录下,所以导入路径是import themes from '@/utils/themes'
。
src -> utils -> elCssVar.json
是获取的element-plus的css变量
switchTheme函数
来控制主题的切换data() { return { currentSkinName: 'darkTheme', themeColorObj: { defaultTheme: { title: '浅色主题' }, darkTheme: { title: '深色主题' } }, themeObj: {} }; }, mounted() { this.switchTheme() }, methods: { // 根据不同的主题类型 获取不同主题数据 switchTheme(type) { // themes 对象包含 defaultTheme、darkTheme 两个属性即默认主题与深色主题 this.currentSkinName = type || 'darkTheme' this.themeObj = themes[this.currentSkinName] this.getsTheColorScale() // 设置css 变量 Object.keys(this.themeObj).map(item => { document.documentElement.style.setProperty(item, this.themeObj[item]) }) }, // // 获取色阶 getsTheColorScale() { const colorList = ['primary', 'success', 'warning', 'danger', 'error', 'info'] const prefix = '--el-color-' colorList.map(colorItem => { for (let i = 1; i < 10; i += 1) { if (i === 2) { // todo 深色变量生成未完成 以基本色设置 this.themeObj[`${prefix}${colorItem}-dark-${2}`] = colorMix(this.themeObj[`${prefix}black`], this.themeObj[`${prefix}${colorItem}`], 1) } else { this.themeObj[`${prefix}${colorItem}-light-${10 - i}`] = colorMix(this.themeObj[`${prefix}white`], this.themeObj[`${prefix}${colorItem}`], i * 0.1) } } }) } }
colorMix函数
仓库示例是在 src -> utils -> tool.js
导出。const colorMix = (color1, color2, weight) => { weight = Math.max(Math.min(Number(weight), 1), 0) let r1 = parseInt(color1.substring(1, 3), 16) let g1 = parseInt(color1.substring(3, 5), 16) let b1 = parseInt(color1.substring(5, 7), 16) let r2 = parseInt(color2.substring(1, 3), 16) let g2 = parseInt(color2.substring(3, 5), 16) let b2 = parseInt(color2.substring(5, 7), 16) let r = Math.round(r1 * (1 - weight) + r2 * weight) let g = Math.round(g1 * (1 - weight) + g2 * weight) let b = Math.round(b1 * (1 - weight) + b2 * weight) r = ("0" + (r || 0).toString(16)).slice(-2) g = ("0" + (g || 0).toString(16)).slice(-2) b = ("0" + (b || 0).toString(16)).slice(-2) return "#" + r + g + b; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。