vue elementui 实现换肤效果
代码
/**
* @Author: 858834013@qq.com
* @Name: changeSkin
* @Date: 2022-02-07
* @Desc:
*/
<template>
<div class="Home">
<div class="pageMain">
<div class="listInfo">
<div v-for="(item,index) in themeList" :key="index" @click="changeSkin(item)">
<item :themeId="themeId" :item="item">
{{ item }}
</item>
</div>
</div>
</div>
<changeTheme v-if="theme" :theme="theme"></changeTheme>
<div class="yangshizhanshi">
<el-button>默认按钮</el-button>
<el-button type="primary">主要按钮</el-button>
<el-button plain>朴素按钮</el-button>
<el-button type="primary" plain>主要按钮</el-button>
</div>
</div>
</template>
<script>
import item from './item'
import changeTheme from './changeTheme'
export default {
name: 'Home',
data() {
return {
// 选中的主题
themeId: '',
// 主题数据
themeList: ['deepBlue', 'Blue', 'purple', 'green'],
themeName: '',
list: [{
name: 'deepBlue',
theme: '#232959'
}, {
name: 'Blue',
theme: '#0657BB'
}, {
name: 'purple',
theme: '#7128CB'
}, {
name: 'green',
theme: '#15A073'
}]
}
},
components: {item, changeTheme},
mounted() {
if (localStorage.getItem('theme')) {
this.themeId = localStorage.getItem('theme')
} else {
this.themeId = 'deepBlue'
}
},
computed: {
theme() {
var name = '#232959'
this.list.forEach((type) => {
if (this.themeName == type.name) {
name = type.theme
}
})
return name
}
},
methods: {
// 换肤
changeSkin(mode) {
this.themeId = mode
this.themeName = mode
if (!mode) {
window.document.documentElement.setAttribute('data-theme', 'blue')
localStorage.setItem('theme', 'blue')
} else {
window.document.documentElement.setAttribute('data-theme', mode)
localStorage.setItem('theme', mode)
}
}
}
}
</script>
<style lang="scss" scoped>
.Home {
width: 100%;
height: calc(100% - 0px);
position: relative;
padding-top: 50px;
.listInfo {
display: flex;
justify-content: flex-start;
align-items: flex-start;
flex-wrap: wrap;
flex-direction: row;
}
}
</style>
changeTheme
/**
* @Author: 858834013@qq.com
* @Name: changeTheme
* @Date: 2022-02-10
* @Desc:
*/
<template>
<div>
</div>
</template>
<script>
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: ''
}
},
computed: {
defaultTheme() {
return this.$store.state.settings.theme
}
},
props: {
theme: {
type: String,
default() {
return ''
}
}
},
watch: {
defaultTheme: {
handler: function(val, oldVal) {
this.theme = val
},
immediate: true
},
async theme(val) {
const oldVal = this.chalk ? this.theme : ORIGINAL_THEME
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('#', ''))
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
}
}
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 = this.updateStyle(innerText, originalCluster, themeCluster)
})
this.$emit('change', val)
}
},
methods: {
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 lang="scss" scoped>
</style>
item
/**
* @Author: 858834013@qq.com
* @Name: item
* @Date: 2022-02-08
* @Desc:
*/
<template>
<div class="skinItem">
<div :class="item" class="skinItem_left"></div>
<div class="skinItem_right">
<div class="skinItem_right_top"></div>
<img v-if="themeId==item" class="active" src="./assets/active.png" alt="">
</div>
</div>
</template>
<script>
export default {
name: 'skinItem',
components: {},
props: {
themeId: {
type: String,
default() {
return ''
}
},
item: {
type: String,
default() {
return ''
}
}
},
data() {
return {
status: ''
}
},
watch: {},
mounted() {
},
methods: {}
}
</script>
<style lang="scss" scoped>
.skinItem {
width: 80px;
height: 80px;
background: #F4F9FD;
box-shadow: 0px 2px 32px 0px rgba(184, 200, 224, 0.22);
border-radius: 8px;
border: 1px solid #D8E0F0;
margin-right: 10px;
margin-bottom: 10px;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: nowrap;
flex-direction: row;
overflow: hidden;
position: relative;
.skinItem_left {
width: 40%;
height: 100%;
background: #253890;
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
.skinItem_left.Blue {
background: #0757BA;
}
.skinItem_left.purple {
background: #7228CC;
}
.skinItem_left.green {
background: #16A172;
}
//'deepBlue', 'Blue', 'purple', 'green'
.skinItem_right_top {
width: 100%;
height: 37px;
background: #FFFFFF;
position: absolute;
top: 0;
left: 0;
}
.skinItem_right {
img {
width: 30px;
height: 22px;
position: absolute;
right: 10px;
bottom: 10px;
}
}
}
</style>