Vue项目中使用Sass实现动态换肤

前一段时间完成了一个项目,该项目要求黑夜和白天两种主题色,可以动态的切换。经过多方的查找研究,终于完成。谨以此文在此记录一下。

此方案涉及Sass的map遍历、函数定义、map存取、混合器等相关知识,具体API详参官网https://www.sass.hk/docs/。

大概的思路就是给html根标签设置一个data-theme属性,通过js切换data-theme的属性值,sass根据此属性来判断使用对应主题变量。

具体实现步骤,以我的项目(dark、light)两种主题为例。

1.定义一个sass文件_themes.scss,存放系统中两种主题的相关颜色变量,文件位置assets目录下,代码如下

//定义全局主题&颜色的map数组,鉴于V5只有白天和晚上的主题,此处仅定义这两种
//切记 所有颜色一式两份儿,务必保证key值统一,key值可以自定义,注意避免$%_之类的,
//与sass自有符号冲突,见文知意即可
//data-theme为dark时,样式引用dark

/* theme variable */
$themes: (
  // light
  light:
    (
      // light theme
      color-primary: #3961f5,
      color-primary-second: #5e81f7,
      color-text-primary: #242f57,
      color-success: #04b78a,
      color-warning: #f2a626,
      color-danger: #ee2e6b,
      background: #fff,
      background-content: #f4f7fc,
      background-color: #eaedf7,
    ),
  //dark theme
  dark:
    (
      color-primary: #3961f5,
      color-primary-second: #5e81f7,
      color-text-primary: #ffffff,
      color-danger: #ee2e6b,
      background: #1d2742,
      background-content: #13192f,
      background-color: #1d2742,
    )
);


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

注意,scss文件名建议用下划线开头,如_themes.scss,防止执行时被编译成单独的css文件,详参sass中文教程(3.1 sass文件导入)。

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

@import "@/style/_themes.scss";
//此处用了sass的map遍历、函数、map存取、混合器等相关知识,
//详细API参考https://www.sass.hk/docs/
//遍历主题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);
  }
}
//获取字体颜色
@mixin font_color($color) {
  @include themeify {
    color: themed($color);
  }
}
//获取边框颜色
@mixin border_color($color) {
  @include themeify {
    border-color: themed($color);
  }
}


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

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

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


到此,完毕了。至于怎么动态切换html的属性data-theme的值,那就驾轻就熟,js怎么干都行了,下面举例一种:

<template>
  <div>
    <p>这里是页面</p>
    <i
      class="iconfont el-ui-Group20"
      @click="handleToggleTheme(0)"
      :class="{ themeSelect: parseInt(index) === 0 }"
    ></i>
    <i
      class="iconfont el-ui-moon"
      @click="handleToggleTheme(1)"
      :class="{ themeSelect: parseInt(index) === 1 }"
    ></i>
  </div>
</template>
 
<script>
export default {
  name: "HelloWorld",
  methods: {
    handleToggleTheme(index) {
      window.document.documentElement.setAttribute(
        "data-theme",
        index ? "dark" : "light"
      );
    }
  }
};

本文实现的动态换肤并非elementUi官网那种随意用ColorPicker实时更改主题的效果,而是为系统预设的几种主题事先配置颜色,然后线上触发更改主题的事件。

本项目当中也使用了element-ui组件,但是在以上变量当中没有更换eleement-ui的组件颜色。而是在eleement-ui的官网当中分别定制dark和light两种主题色,通过link标签引入到入口文件index.html当中:

然后更换link标签属性href路径来改变element-ui的主题色:

 handleToggleTheme(index) {
      this.index = index;
      const elementTheme = document.getElementById('elementTheme');
      // console.log(elementTheme.getAttribute('href'));
      // index is 0, theme is light
      // index is 1, theme is dark
      let theme = index === 1 ? 'dark' : 'light';
      this.$store.dispatch('handleActionTheme', index);
      elementTheme.setAttribute(
        'href',
        `${this.publicPath}theme/${theme}/theme/index.css`
      );
      window.localStorage.setItem('theme', index);
      window.document.documentElement.setAttribute('data-theme', theme);
      this.reload();
    }
  },
 
 
 
 

猜你喜欢

转载自blog.csdn.net/sinat_36728518/article/details/115727605#comments_21827868
今日推荐