scss配置主题

$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>

在Vue项目中,如果你想自定义Element Plus组件的样式并使用SCSS,你可以按照以下步骤操作: 1. **安装依赖**: 首先确保已经在项目中安装了Vue和Element Plus,如果还没有,可以使用`npm install vue-element-plus` 或者 `yarn add vue-element-plus`. 2. **引入scss预处理器**: 如果你的项目尚未配置SCSS,需要安装并配置Sass。对于 Vue CLI 项目,可以安装`@vue/cli-plugin-sass`,然后运行`vue add sass`。 ```bash npm install -D @vue/cli-plugin-sass # 或者 yarn add -D @vue/cli-plugin-sass ``` 然后在`.vue`文件中添加`:scoped`属性到`<style>`标签内,让样式仅作用于当前组件。 3. **创建主题变量**: 在`src/styles/theme.scss` 或者单独创建一个scss文件中,定义组件的通用颜色、尺寸等变量。 4. **修改组件样式**: 找到Element Plus组件对应的scss文件,通常在`element-plus/packages/theme-chalk/src/components/_variables.scss`中。复制你想要覆盖的部分,如`.el-button` 类名,并在你的项目中定义新的样式规则。例如: ```scss .your-component-name { // 自定义你的样式,如字体大小、背景色等 font-size: 14px; background-color: #3f51b5; // 修改为自定义颜色 } ``` 5. **应用到元素上**: 在Vue组件中导入主题样式,并将自定义样式的类名应用到Element Plus组件上。例如: ```html <template> <el-button class="your-component-name">点击我</el-button> </template> <script setup> import './styles/theme.scss'; </script> ``` 6. **CSS Modules** (如有使用): 如果你的项目启用了CSS Modules,记得在import时加上模块前缀,如`@/components/theme.scss`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值