sass换肤

文章介绍了如何使用SCSS和JavaScript在Vue项目中实现动态主题切换,通过处理`data-theme`属性和自定义混入函数,根据不同主题提供相应的颜色和样式。
摘要由CSDN通过智能技术生成
定义主题
$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,
    
    )
);
新建_handle.scss
@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) {
	//map-get 根据对应的key值返回map中对应的值
  //map-get($map, $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;
    }
}
js处理
<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>
<scritp>
theme(type) {
    // vue2 this.$store.commit('upDate', {themeType: type});
    window.document.documentElement.setAttribute( "data-theme", type );
}
</script>
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值