vue项目主题切换

在 assets 目录下新建 theme文件夹 并创建 color.less theme.less

color.less

@import url('./theme.less'); // 顶部导航栏


// 默认的颜色 工业风 暗色系
.theme_default {
    .theme();
}


// 白色
.theme_white {
    .theme( // 主题主色
        #FFFFFF,
        // 主题高亮主色
        #4F79EF,
        // 全局文本主色
        rgba(0, 0, 0, 0.6),
        // 全局标题主色
        rgba(0, 0, 0, 0.9),
    )
}


// 黑色
.theme_dark {
    .theme( // 主题主色
        #242424,
        // 主题高亮主色
        #4F79EF,
        // 全局文本主色
        rgba(255, 255, 255, 0.6),
        // 全局标题主色
        rgba(255, 255, 255, 0.9),
    )
}

theme.less

/**
    主题样式通用文件
*/
.theme(@theme-global-bg: rgba(8, 30, 61, 1), // 主题主颜色
    @primary-color: #70f7ff, // 主题高亮主色
    @global-text-color: #70f7ff, // 全局文本主色
    @global-title-color: #ffffff, // 全局标题主色
) {


    // 全局文字主色
    .theme-global-text-color {
        color: @global-text-color !important;
    }


    // 全局标题颜色
    .theme-global-title-color {
        font-weight: 400;
        color: @global-title-color !important;
    }


    // 全局关键色
    .theme-primary-color {
        color: @primary-color !important;
    }
    // 全局主背景色
    .theme-global-bg {
        background: @theme-global-bg !important;
    }
}

2、 在main.js中引入 clolor.less

import './assets/theme/color.less'; // global style

3、在项目初始化执行

// 初始化主题
 var bodyClass = localStorage.getItem('themeColor') ? localStorage.getItem('themeColor') : 'theme_default';
  document.body.className = bodyClass;

4、 主题切换

//  主题列表
colorList: [
        { key: '工业风', color: '#234A74', type: 'theme_default' },
        { key: '白色系', color: '#ffffff', type: 'theme_white' },
        { key: '黑色系', color: '#242424', type: 'theme_dark' },
      ],

主题切换

changeColor(type) {
      const currentThemeColor= localStorage.getItem('themeColor') ? localStorage.getItem('themeColor') : 'theme_default';
      if (currentThemeColor!== type) {
        document.body.className = type;
        localStorage.setItem('themeColor',type)
      }
    },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Vue项目多模块集成主题切换的demo,使用了Sass作为CSS预处理器和Element UI作为UI库: 1. 安装Element UI和Sass ``` npm install element-ui sass sass-loader ``` 2. 创建一个theme.scss文件,定义变量和样式 ```scss // 定义主题变量 $primary-color: #409EFF; $success-color: #67C23A; $warning-color: #E6A23C; $error-color: #F56C6C; $info-color: #909399; // 根据变量定义样式 .el-button { background-color: $primary-color; border-color: $primary-color; &:hover, &:focus { background-color: lighten($primary-color, 10%); border-color: lighten($primary-color, 10%); } } .el-alert { background-color: $info-color; border-color: $info-color; } ``` 3. 在main.js文件中添加以下代码,引入Element UI和theme.scss文件 ```js import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; import './theme.scss'; Vue.use(ElementUI); ``` 4. 创建一个ThemeSwitcher组件,用来切换主题 ```html <template> <div> <el-radio-group v-model="theme" @change="changeTheme"> <el-radio-button label="theme-light">浅色主题</el-radio-button> <el-radio-button label="theme-dark">深色主题</el-radio-button> </el-radio-group> </div> </template> <script> export default { data() { return { theme: 'theme-light' } }, methods: { changeTheme() { const oldLink = document.getElementById('theme'); const newLink = document.createElement('link'); newLink.id = 'theme'; newLink.rel = 'stylesheet'; newLink.href = `/themes/${this.theme}.css`; document.head.replaceChild(newLink, oldLink); } } } </script> ``` 5. 创建两个主题样式文件,分别为theme-light.css和theme-dark.css,放在public/themes文件夹下 6. 在App.vue中引入ThemeSwitcher组件,并在页面中使用 ```html <template> <div id="app"> <theme-switcher></theme-switcher> <router-view></router-view> </div> </template> <script> import ThemeSwitcher from './components/ThemeSwitcher.vue'; export default { components: { ThemeSwitcher } } </script> ``` 这样就可以实现多模块集成主题切换了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值