scss配置主题

这篇文章介绍了如何使用SCSS(SassyCSS)的@mixinthemeify混入来管理light和dark主题下的颜色配置,通过HTMLdata-theme属性动态切换样式。示例展示了如何应用背景色和边框色,以及如何根据主题状态改变文档元素的样式。
$themes: (
  light: (
     font_color: #fff,
  ),
  dark: (
     font_color: #000,
  ),
);

@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: themed($color) !important;
  }
}

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

@mixin stroke_color($color) {
  @include themeify {
    stroke: themed($color) !important;
  }
}

@mixin font_fill($color) {
  @include themeify {
    fill: themed($color) !important;
  }
}
@mixin border($color) {
  @include themeify {
    border: themed($color) 1px solid;
  }
}
@mixin border_color($color) {
  @include themeify {
    border-color: themed($color) !important;
  }
}
@mixin border_bottom_color($color) {
  @include themeify {
    border-bottom-color: themed($color) !important;
  }
}
@mixin border-bottom($color) {
  @include themeify {
    border-bottom: themed($color) 1px solid !important;
  }
}

 上面是theme.scss 文件 light,dark 黑白主题配置颜色。

在项目中使用案例如下:

 @include background_color('font_color');  //背景颜色

 @include border_color('font_color');  //边框颜色

切换data-theme:

   if (this.$store.state.dark) {
      window.document.documentElement.setAttribute('data-theme', 'dark');
    } else {
      window.document.documentElement.setAttribute('data-theme', 'light');
    }

黑:

<html lang="" data-theme="dark"></html>

白:

<html lang="" data-theme="light"></html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值