Vue使用Less与Scss实现主题切换方法详细讲解
作者:苪玫儿呀 发布时间:2024-04-27 15:48:13
一、Less/Scss变量换肤
具体实现:
1、初始化vue项目
2、安装插件:
npm install style-resources-loader -D
npm install vue-cli-plugin-style-resources-loader -D
当然也要安装less、less-loader等插件,具体的安装配置,请自行google
3、新建theme.less文件用于全局样式配置。在src目录下新建theme文件夹,在这个文件夹下新建theme.less文件。具体如下:
/src/theme/theme.less
// 默认的主题颜色
@primaryColor: var(--primaryColor, #000);
@primaryTextColor: var(--primaryTextColor, green);
// 导出变量
:export {
name: "less";
primaryColor: @primaryColor;
primaryTextColor: @primaryTextColor;
}
4、配置vue.config.js文件,实现全局使用变量实现换肤
const path = require("path");
module.exports = {
pluginOptions: {
"style-resources-loader": {
preProcessor: "less",
patterns: [
// 这个是加上自己的路径,不能使用(如下:alias)中配置的别名路径
path.resolve(__dirname, "./src/theme/theme.less"),
],
},
},
};
5、具体的使用:
<template>
<div class="hello">
<p>我是测试文字</p>
</div>
</template>
<script>
export default {
name: "HelloWorld",
};
</script>
<style scoped lang="less">
.hello {
p {
color: @primaryTextColor;
}
}
</style>
备注:如果是用scss也基本同以上用法,只是scss的变量名用‘$’作为前缀,less使用@
至此,我们已经实现了静态更换皮肤,那如何实现动态换肤呢,最重要的就是以下的文件了。
我们可以多配置几种默认主题
6、在theme文件夹下新建model.js文件,用于存放默认主题
// 一套默认主题以及一套暗黑主题
// 一套默认主题以及一套暗黑主题
export const themes = {
default: {
primaryColor: `${74}, ${144},${226}`,
primaryTextColor: `${74}, ${144},${226}`,
},
dark: {
primaryColor: `${0},${0},${0}`,
primaryTextColor: `${0},${0},${0}`,
},
};
7、实现动态切换:
在/src/theme文件夹下新建theme.js文件,代码如下:
import { themes } from "./model";
// 修改页面中的样式变量值
const changeStyle = (obj) => {
for (let key in obj) {
document
.getElementsByTagName("body")[0]
.style.setProperty(`--${key}`, obj[key]);
}
};
// 改变主题的方法
export const setTheme = (themeName) => {
localStorage.setItem("theme", themeName); // 保存主题到本地,下次进入使用该主题
const themeConfig = themes[themeName];
// 如果有主题名称,那么则采用我们定义的主题
if (themeConfig) {
localStorage.setItem("primaryColor", themeConfig.primaryColor); // 保存主题色到本地
localStorage.setItem("primaryTextColor", themeConfig.primaryTextColor); // 保存文字颜色到本地
changeStyle(themeConfig); // 改变样式
} else {
let themeConfig = {
primaryColor: localStorage.getItem("primaryColor"),
primaryTextColor: localStorage.getItem("primaryTextColor"),
};
changeStyle(themeConfig);
}
};
8、切换主题
this.setTheme('dark')
二、element-UI组件的换肤
1、一般elementUI主题色都有这样一个文件element-variables.scss:
/**
* I think element-ui's default theme color is too light for long-term use.
* So I modified the default color and you can modify it to your liking.
**/
/* theme color */
$--color-primary: #1890ff;
$--color-success: #13ce66;
$--color-warning: #ffba00;
$--color-danger: #ff4949;
// $--color-info: #1E1E1E;
$--button-font-weight: 400;
// $--color-text-regular: #1f2d3d;
$--border-color-light: #dfe4ed;
$--border-color-lighter: #e6ebf5;
$--table-border: 1px solid #dfe6ec;
/* icon font path, required */
$--font-path: "~element-ui/lib/theme-chalk/fonts";
@import "~element-ui/packages/theme-chalk/src/index";
// the :export directive is the magic sauce for webpack
// https://www.bluematador.com/blog/how-to-share-variables-between-js-and-sass
:export {
theme: $--color-primary;
}
2、main.js中引用
import './styles/element-variables.scss'
3、在store文件夹下新建settings.js文件,用于页面基础设置
import variables from '@/styles/element-variables.scss'
const state = {
theme: variables.theme
}
const mutations = {
CHANGE_SETTING: (state, { key, value }) => {
// eslint-disable-next-line no-prototype-builtins
if (state.hasOwnProperty(key)) {
state[key] = value
}
}
}
const actions = {
changeSetting({ commit }, data) {
commit('CHANGE_SETTING', data)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
4、一般换肤都是需要有个颜色选择器,用于皮肤设置
在src目录下新建ThemePicker文件夹,新建index.vue文件。
<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 // 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
}
},
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 $message = this.$message({
message: ' Compiling the theme',
customClass: 'theme-message',
type: 'success',
duration: 0,
iconClass: 'el-icon-loading'
})
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)
$message.close()
}
},
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>
.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>
5、在使用ThemePicker组件的位置,去调用vuex中的changeSetting函数
<theme-picker style="float: right;height: 26px;margin: -3px 8px 0 0;" @change="themeChange" />
import ThemePicker from '@/components/ThemePicker'
export default {
components: { ThemePicker },
methods:{
themeChange(val) {
this.$store.dispatch('settings/changeSetting', {
key: 'theme',
value: val
})
}
}
}
至此,就可以实现elementUI组件的换肤功能了
总结:其实上边两种方式换肤的实现思路都差不多,下边那篇自己理解得不是很好,欢迎补充
来源:https://blog.csdn.net/hbmern/article/details/129172426


猜你喜欢
- 安装laravel框架命令行cd进入指定目录下,执行composer create-project --prefer-dist larave
- 前言大家好最近python爬虫有点火啊,啥python爬取马保国视频……我也来凑个热闹,今天我们来试着做个翻译软件……不是不是,说错了,今天
- 一、数据引擎简介在MySQL 5.1中,MySQL AB引入了新的插件式存储引擎体系结构,允许将存储引擎加载到正在运新的MySQL
- set转成list方法如下: list转成set方法如下:s = set('12342212') &n
- Vue3 ref获取DOM元素<div ref="divBox">Hello</div>impo
- 产生跨域问题的原因跨域问题是浏览器同源策略限制,当前域名的js只能读取同域下的窗口属性。跨域问题产生的场景当要在在页面中使用js获取其他网站
- 前言本文主要给大家介绍了关于python中Numpy和Pandas使用的相关资料,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介
- 有些时候因为某些原因(如本地机器资源不足、数据不能离网等),需要使用本地电脑连接远程服务器进行开发工作,在这里记录下如何在远程Linux上配
- JS如何从一个数组中随机取出一个元素或者几个元素。假如数组为var items = ['1','2',
- 前言系统自带的数据表格,使用时通过sns.load_dataset('表名称')即可,结果为一个DataFrame。prin
- 一、系统资源使用限制的必要性探讨对于一个脚本,最基础的限制是要限制单进程实例以保证了不会存在多个进程实例、在运行程序主体逻辑前检测系统资源剩
- 需求描述:在公司老旧系统里,数据库表很多,但是在设计之初并没有建立好关系图,导致新人刚入职,面对N个库,每个库几百张表,很不方便。例如:公司
- 前言:本系列文章主要参考了《Ultimate ASP.NET Core 3 Web API》一书,对原文进行了翻译,同时适当删减、修改了一部
- 处理过滤Apache日志文件access_test.log文件内容27.19.74.143 - - [30/May/2015:17:38:2
- Pythonpython 真的太好用了,但是它真的好慢啊(哭死) ; C++ 很快,但是真的好难写啊,此生能不碰它就不碰它。老天啊,有没有什
- 创建项目django-admin startproject meiduo_mall添加工程完整结构包启动前端python -m http.s
- librosa是处理音频库里的opencv,使用python脚本研究音频,先安装三方库librosa。如下通过清华镜像源安装librosa;
- 如下所示:a = [0, 1, 2, 3, 4, 0, 2, 3, 6, 7, 5]selected = [x for x in a if
- 瞎鼓捣系列~Numpy + matplotlib 画一个魔方前言NumPy是Python科学计算的基本包。它是一个Python库,提供了多维
- Vuex使用单一状态树(一个对象就包含了全部的应用层级状态),它作为唯一数据源存在,每个应用仅仅有一个store实例。单一状态树使得我们能够