一键换肤(Vue + Scss 动态切换主题颜色)

Vue + Scss 动态切换主题颜色实现换肤

  • 首先需要给项目下载配置 scss
  • 在 vue 项目全局中引入 scss
  • 准备工作已经完成

根据预设的配色方案,在前端实现动态切换系统主题颜色。

大概的思路就是给 html 根标签设置一个 data-theme 属性,然后通过 js 切换 data-theme 的属性值,scss 根据此属性来判断使用对应主题变量。这里可以选择 Vuex 或接口来保存用户选择的主题。

一、首先需要给项目下载配置 scss

1、安装依赖

npm install node-sass sass-loader --save-dev

2、找到 build 中 webpack.base.conf.js 在 rules 中添加 scss 规则

{
    test: /\.scss$/,
    loaders: ['style', 'css', 'sass'v]
}

3、在 vue 文件中使用

<style lang='scss' scoped>

</style>

二、在vue项目全局中引入scss

1、安装 sass-resources-loader

npm install sass-resources-loader --save-dev

2、然后修改build中的utils.js

scss: generateLoaders('sass')

修改为:

scss: generateLoaders('sass').concat(
    {
        loader: 'sass-resources-loader',
        options: {
            //你自己的scss全局文件的路径
            resources: path.resolve(__dirname, '../src/style/_common.scss')
        }
    }
)

三、准备工作已经完成,接下来根据需求设置

1、新建一个 scss 文件 _themes.scss,里面可以配置不同的主题配色方案

//_themes.scss
//当HTML的data-theme为dark时,样式引用dark
//data-theme为其他值时,就采用组件库的默认样式
//这里我只定义了两套主题方案,想要再多只需在`$themes`里加就行了
//注意一点是,每套配色方案里的key可以自定义但必须一致,不然就会混乱

$themes: (

    light: (
        //字体
        font_color1: #414141,
        font_color2: white,
        
        //背景
        background_color1: #fff,
        background_color2: #f0f2f5,
        background_color3: red,
        background_color4: #2674e7,
        
        //边框
        border_color1: #3d414a,
    
    ),
    
    dark: (
        //字体
        font_color1: #a7a7a7,
        font_color2: white,
        
        //背景
        background_color1: #1b2531,
        background_color2: #283142,
        background_color3: #1e6ceb,
        background_color4: #323e4e,
    
        //边框
        border_color1: #3d414a,
    
    )
);

这里定义了一个map–$themes,分别存放对应主题的颜色变量集合。

注意,scss 文件名建议用下划线开头,如 _themes.scss,防止执行时被编译成单独的 css 文件。

2、定义另外一个 sass 文件 _handle.scss 来操作1中的 $theme 变量(当然两个文件可以合并,分开写是想把配置和操作解耦)

@import "./_themes.scss";

//遍历主题map
@mixin themeify {
    @each $theme-name, $theme-map in $themes {
        //!global 把局部变量强升为全局变量
        $theme-map: $theme-map !global;
        //判断html的data-theme的属性值  #{}是sass的插值表达式
        //& sass嵌套里的父容器标识   @content是混合器插槽,像vue的slot
        [data-theme="#{$theme-name}"] & {
            @content;
        }
    }
}

//声明一个根据Key获取颜色的function
@function themed($key) {
    @return map-get($theme-map, $key);
}

//获取背景颜色
@mixin background_color($color) {
    @include themeify {
        background-color: themed($color)!important;
    }
}

//获取字体颜色
@mixin font_color($color) {
    @include themeify {
        color: themed($color)!important;
    }
}

//获取边框颜色
@mixin border_color($color) {
    @include themeify {
        border-color: themed($color)!important;
    }
}
  • themeify 方法用于获取 HTML 的 data-theme 值。
  • themed 方法用于根据 HTML 的 data-theme 值及调用者传过来的 key 去 _themes.scss 里获取相应的颜色。

上面可以根据自己的使用场景自定义混入器,上面只定义了三个常用的背景&边框&字体的颜色。

3、具体在 vue 中使用,直接引入对应混入器就好,取哪个颜色,传哪个key,就这么简单

<style lang="scss" scoped>
    @import "@/style/_handle.scss";
    .common-util {
        font-size: 18px;
        @include font_color("font_color1");
        @include background_color("background_color1");
        @include border_color("border_color1");
    }
</style>

4、使用 js 动态切换 HTML 的属性 data-theme 的值

//HTML

<DropdownMenu slot="list">
    <DropdownItem @click.native="theme('iview')">默认</DropdownItem>
    <DropdownItem @click.native="theme('light')">浅色</DropdownItem>
    <DropdownItem @click.native="theme('dark')">深色</DropdownItem>
</DropdownMenu>

//JS

//换主题
theme(type) {
    this.$store.commit('upDate', {themeType: type});
    window.document.documentElement.setAttribute( "data-theme", type );
}

切换后会发现你的 css 选择器前面多了一些东西 [data-theme=“dark”]

[data-theme="dark"] .ivu-layout-sider,
[data-theme="dark"] .ivu-menu-light,
[data-theme="dark"] .ivu-layout-header {
    background-color: #283142!important;
}

到这里已全部实现功能。


参考资料:
参考1:https://juejin.cn/post/7341797865108766746?from=search-suggest#heading-3
参考2:Vue + Scss 动态切换主题颜色实现换肤:https://blog.csdn.net/Echocyd/article/details/106350793
参考3:一文搞懂前端多主题适配方案:https://juejin.cn/post/7049384448256639006?from=search-suggest
参考4:前端页面主题切换:https://juejin.cn/post/7042605241023332389?from=search-suggest

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值