本文参考的是vue-element-admin里的主题切换,并加以补充了一些问题,要是有不完善的地方,希望大家能帮忙指出。
在components目录中创建创建:ThemePicker/index.vue
这边主要的功能是选择颜色块之后,将获取到的element中相对应的主题颜色,存放到style标签页之后,进行replace替换所选择的颜色。
<template>
<el-color-picker v-model="theme" :predefine="['#409EFF', '#1890ff', '#304156', '#212121', '#11a983', '#13c2c2', '#6959CD', '#f5222d']" class="theme-picker" popper-class="theme-picker-dropdown" />
</template>
<script>
const version = require('element-ui/package.json').version
const ORIGINAL_THEME = '#409EFF'
import { setThemeChalk, getThemeCluster, updateStyle, getTheme } from '@/utils/auth'
export default {
data() {
return {
chalk: '',
theme: getTheme() ?? ORIGINAL_THEME,
}
},
watch: {
async theme(val) {
const oldVal = this.chalk ? this.theme : ORIGINAL_THEME
if (typeof val !== 'string') return
const themeCluster = getThemeCluster(val.replace('#', ''))
const originalCluster = getThemeCluster(oldVal.replace('#', ''))
const $message = this.$message({
message: ' 正在切换主题',
customClass: 'theme-message',
type: 'success',
duration: 0,
iconClass: 'el-icon-loading',
})
const getHandler = (variable, id) => {
return () => {
const originalCluster = getThemeCluster(ORIGINAL_THEME.replace('#', ''))
const newStyle = 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
}
}
if (!this.chalk) {
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 = updateStyle(innerText, originalCluster, themeCluster)
})
this.$emit('change', val, themeCluster)
$message.close()
},
},
methods: {
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{[^}]+}/, '')
setThemeChalk(this[variable])
resolve()
}
}
xhr.open('GET', url)
xhr.send()
})
},
},
}
</script>
<style lang="scss">
.theme-message,
.theme-picker-dropdown {
z-index: 99999 !important;
}
.theme-picker .el-color-picker__trigger {
height: 26px !important;
width: 26px !important;
margin-top: 14px;
padding: 2px;
}
.theme-picker-dropdown .el-color-dropdown__link-btn {
display: none;
}
.el-color-picker__color-inner {
background-color: $commonColor !important;
}
</style>
在utils目录中创建创建:auth.js
//主题颜色
export function setTheme (val) {
return localStorage.setItem('theme_color', val)
}
export function getTheme () {
return localStorage.getItem('theme_color')
}
//主题element文件
export function setThemeChalk (val) {
return localStorage.setItem('theme_chalk', val)
}
export function getThemeChalk () {
return localStorage.getItem('theme_chalk')
}
//获取相对应的颜色
export function 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
}
//更改替换主题颜色
export function updateStyle (style, oldCluster, newCluster) {
let newStyle = style
oldCluster.forEach((color, index) => {
newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])
})
return newStyle
}
引用组件
<template>
<!-- 主题切换 -->
<theme-picker class="right-menu-item hover-effect" @change="themeChange"></theme-picker>
</template>
<script>
import ThemePicker from '@/components/ThemePicker'
import { setTheme } from '@/utils/auth'
export default {
components: {
ThemePicker,
},
methods: {
themeChange(val, valJSON) {
//这边的代码后面讲解
document.getElementsByTagName('body')[0].style.setProperty('--common-color', val)
document.getElementsByTagName('body')[0].style.setProperty('--defaultbutton-border-hover', valJSON[valJSON.length - 4])
document.getElementsByTagName('body')[0].style.setProperty('--defaultbutton-bac-hover', valJSON[valJSON.length - 2])
//将拿到的颜色存到localstorage里
setTheme(val)
},
},
}
</script>
到这儿,element组件的主题切换就完成了。
那如果我们自己写的样式里的颜色应该怎么解决呢?
在styles目录中创建:variables.scss
:root {
--common-color: #409eff;
--defaultbutton-bac-hover: #ecf5ff;
--defaultbutton-border-hover: #c6e2ff;
}
// 按钮公共颜色
$commonColor: var(--common-color);
$defaultbuttonBorderHover: var(--defaultbutton-border-hover);
$defaultbuttonBacHover: var(--defaultbutton-bac-hover);
在themeChange方法中,就要相对应的更改这边的变量
document.getElementsByTagName('body')[0].style.setProperty('--common-color', val)
document.getElementsByTagName('body')[0].style.setProperty('--defaultbutton-border-hover', valJSON[valJSON.length - 4])
document.getElementsByTagName('body')[0].style.setProperty('--defaultbutton-bac-hover', valJSON[valJSON.length - 2])
到这儿基础的主题切换已经完成了,但是会有个问题,那就是在我们页面去加载之后再去获取localstorage里的颜色并替换,会先出现原本颜色,然后再变成新颜色,有个一两秒的延迟。所以我们需要在App.vue文件中先初始化localstorage中的颜色。
<script>
import { getTheme, getThemeChalk, getThemeCluster, updateStyle } from '@/utils/auth'
const ORIGINAL_THEME = '#409EFF'
export default {
name: 'App',
beforeMount() {
let default_theme = getTheme()
if (default_theme) {
let default_theme_chalk = getThemeChalk()
const themeCluster = getThemeCluster(default_theme.replace('#', ''))
const originalCluster = getThemeCluster(ORIGINAL_THEME.replace('#', ''))
const newStyle = updateStyle(default_theme_chalk, originalCluster, themeCluster)
let styleTag = document.getElementById('chalk-style')
if (!styleTag) {
let styleTag = document.createElement('style')
styleTag.setAttribute('id', 'chalk-style')
document.head.appendChild(styleTag)
}
document.getElementById('chalk-style').innerText = newStyle
document.getElementsByTagName('body')[0].style.setProperty('--common-color', default_theme)
document.getElementsByTagName('body')[0].style.setProperty('--defaultbutton-border-hover', themeCluster[themeCluster.length - 4])
document.getElementsByTagName('body')[0].style.setProperty('--defaultbutton-bac-hover', themeCluster[themeCluster.length - 2])
}
},
}
</script>