vue项目皮肤切换方案


vue项目有换肤的需求,那么如何换肤呢。一般是准备两份黑白版UI设计色调。采用 CSS var() 函数自定义变量属性调用。 定义一个名为 "--background" 的属性,然后使用 var(--background) 函数调用该属性。思路就是, 切换皮肤时,切换不同的不同皮肤的自定义属性值。默认黑色背景,每次切换皮肤,存储本地存储,刷新后皮肤保持一致

1、theme.less

theme-b.less

:root {
  --background: #000;
}

theme-w.less

:root.white{
  --background: #fff;
}

theme.less

@import "theme-b.less";
@import "theme-w.less";

2、引用theme.less

App.vue

<template>
  <div id="app">
    <img ref="test" alt="Vue logo" src="./assets/logo.png">
  </div>
</template>

<script>

export default {
  name: 'App',
}
</script>

<style>
@import "./less/theme.less";
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  height: 100vh;
  background: var(--background);
}
</style>

在这里插入图片描述

3、皮肤切换

theme.js

const theme = window.theme || {};

theme.changeTheme = function (type) {
  switch (type) {
    case 'white':
      document.documentElement.classList.add('white');
      break;
    case 'black':
      document.documentElement.classList.remove('white');
      break;
    default:
      document.documentElement.classList.remove('white');
      break;
  }
}

// 初始化判断皮肤
theme.detectSelectedTheme = function () {
  const type = localStorage.getItem('theme') || '';
  localStorage.setItem('theme', type);
  theme.changeTheme(type);
}

export default theme;

main.js

import Vue from 'vue'
import App from './App.vue'
import theme from './utils/theme'
// 初始化皮肤
theme.detectSelectedTheme();
Vue.prototype.themeUtils = theme;
Vue.config.productionTip = false

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

App.vue

<template>
  <div id="app">
    <img ref="test" alt="Vue logo" src="./assets/logo.png">
    <button @click="changeTheme()">切换</button>
  </div>
</template>

<script>

export default {
  name: 'App',
  methods:{
    changeTheme(){
      const type = localStorage.getItem('theme') == 'white' ? 'black' : 'white';
      localStorage.setItem('theme', type)
      this.themeUtils.changeTheme(type)
    }
  }
}
</script>

<style>
@import "./less/theme.less";
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  height: 100vh;
  background: var(--background);
}
</style>

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值