vue 结合scss实现前台主题颜色动态变化

1、安装scss,参考文章:

vue项目中设置全局引入scss,使每个组件都可以使用变量 - 简书在Vue项目中使用scss,如果写了一套完整的有变量的scss文件。那么就需要全局引入,这样在每个组件中使用。 可以在mian.js全局引入,下面是使用方法。 1: 安装no...https://www.jianshu.com/p/2129f7d704292、在 assets/styles下新建.scss文件,定义想要变化的颜色和主题。(只列举部分颜色)

// -------------------------------更换的系统主题颜色2(Standard)-----------------------------------//
// 菜单所有未选中文字样式
$menuTextStandard: #333333;
// 一级菜单样式
$menuBgStandard: #ffffff;
// 一级菜单鼠标悬浮
$menuHoverStandard: #f7f7f7;
// 一级菜单选中时文字样式
$subMenuActiveTextStandard: #82ba00;
// 选中背景:
$subMenuActiveBgStandard: #e6f1cc;
// -------------------------------更换的系统主题颜色3(StandardRed)-----------------------------------//
// 菜单所有未选中文字样式
$menuTextStandardRed: #333333;
// 一级菜单样式
$menuBgStandardRed: #ffffff;
// 一级菜单鼠标悬浮
$menuHoverStandardRed: #f7f7f7;
// 一级菜单选中时文字样式
$subMenuActiveTextStandardRed: #911844;
// 选中背景:
$subMenuActiveBgStandardRed: #e9d1da;
// -------------------------------更换的系统主题颜色4(StandardSkyBlue)-----------------------------------//
// 菜单所有未选中文字样式
$menuTextStandardSkyBlue: #333333;
// 一级菜单样式
$menuBgStandardSkyBlue: #ffffff;
// 一级菜单鼠标悬浮
$menuHoverStandardSkyBlue: #f7f7f7;
// 一级菜单选中时文字样式
$subMenuActiveTextStandardSkyBlue: #008299;
// 选中背景:
$subMenuActiveBgStandardSkyBlue: #cce6eb;

3、为需要切换的5个颜色在下面定义方法动态切换颜色:(注意部分样式要加 import 才会生效)

@mixin menuText($color) {
  color: $color;

  //   /*判断匹配*/
  [data-theme="standard"] & {
    color: $menuTextStandard;
  }

  [data-theme="standardRed"] & {
    color: $menuTextStandardRed;
  }

  [data-theme="standardSkyBlue"] & {
    color: $menuTextStandardSkyBlue;
  }
}

@mixin menuBg($color) {
  background-color: $color !important;

  //   /*判断匹配*/
  [data-theme="standard"] & {
    background-color: $menuBgStandard !important;
  }

  [data-theme="standardRed"] & {
    background-color: $menuBgStandardRed !important;
  }

  [data-theme="standardSkyBlue"] & {
    background-color: $menuBgStandardSkyBlue !important;
  }
}

@mixin menuHover($color) {
  background-color: $color;

  //   /*判断匹配*/
  [data-theme="standard"] & {
    background-color: $menuHoverStandard;
  }

  [data-theme="standardRed"] & {
    background-color: $menuHoverStandardRed;
  }

  [data-theme="standardSkyBlue"] & {
    background-color: $menuHoverStandardSkyBlue;
  }
}

@mixin subMenuActiveText($color) {
  color: $color !important;

  //   /*判断匹配*/
  [data-theme="standard"] & {
    color: $subMenuActiveTextStandard !important;
  }

  [data-theme="standardRed"] & {
    color: $subMenuActiveTextStandardRed !important;
  }

  [data-theme="standardSkyBlue"] & {
    color: $subMenuActiveTextStandardSkyBlue !important;
  }
}

@mixin subMenuActiveBg($color) {
  background-color: $color !important;

  //   /*判断匹配*/
  [data-theme="standard"] & {
    background-color: $subMenuActiveBgStandard !important;
  }

  [data-theme="standardRed"] & {
    background-color: $subMenuActiveBgStandardRed !important;
  }

  [data-theme="standardSkyBlue"] & {
    background-color: $subMenuActiveBgStandardSkyBlue !important;
  }

}

4、在main.js 中引入文件:

import './assets/styles/variables.scss'

5、在所有页面需要变色的颜色上使用:

// color:#f7f7f7  改为:
@include menuText();  

6、App.vue 中一键全局更改主题颜色:

 function setAttribute(theme) {
      window.document.documentElement.setAttribute("data-theme", theme);
 }
 setAttribute("standard");  // 应用主题2 
 setAttribute("standardRed");  // 应用主题3

本文参考链接:

vue+scss动态改变主题颜色 - 就这样吧丶 - 博客园1、新建.scss后缀公用文件,放在assets或者其他地方都可以 /*需要切换的颜色变量*/ $color-primary1:#1776E1; /* 更换的颜色 */ $color-primary2https://www.cnblogs.com/wtwebskill/p/11812687.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以通过以下步骤实现 Vue + Element UI + SCSS 切换颜色的功能: 1. 在 Vue 的 `data` 中定义一个变量 `color` 来存储当前的颜色值,以及定义一个数组 `colors` 用来存储可选的颜色值。 ```javascript data() { return { color: '#409EFF', colors: ['#409EFF', '#67C23A', '#E6A23C', '#F56C6C'] } } ``` 2. 在模板中使用 Element UI 的 `el-color-picker` 组件来展示当前颜色,并绑定 `color` 变量。 ```html <el-color-picker v-model="color"></el-color-picker> ``` 3. 使用 SCSS 定义不同颜色的样式,并使用 Vue 的计算属性来动态生成样式。 ```scss $primary-color: #409EFF; $success-color: #67C23A; $warning-color: #E6A23C; $error-color: #F56C6C; // 定义主题颜色 @mixin theme-color($color) { --color-primary: #{$color}; --color-success: lighten(#{$color}, 15%); --color-warning: lighten(#{$color}, 30%); --color-error: lighten(#{$color}, 45%); } // 默认主题颜色 :root { @include theme-color($primary-color); } // 其他主题颜色 .theme-color { @each $color in $colors { &[data-color="#{$color}"] { @include theme-color($color); } } } ``` 在上面的代码中,使用了 SCSS 的 `@mixin` 和 `@each` 来定义不同颜色的样式,其中 `--color-primary`、`--color-success`、`--color-warning` 和 `--color-error` 是 Element UI 中预定义的 CSS 变量,用于控制不同组件的颜色。 还定义了一个 `theme-color` 类,用来动态生成主题颜色,并使用 `&[data-color="#{$color}"]` 来指定不同颜色对应的样式。 4. 在模板中使用计算属性来动态生成样式类名,并将其绑定到根元素上。 ```html <div :class="'theme-color ' + color" data-color="color"></div> ``` 在上面的代码中,使用了 `:class` 绑定了一个计算属性,其中 `theme-color` 是样式类名前缀,用于指定主题颜色,`color` 是当前选择的颜色值。 至此,一个简单的 Vue + Element UI + SCSS 切换颜色的功能就实现了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值