创建scss文件
一. 首先,引入scss依赖(node-sass, sass-loader)
$themes: (
basic: (
basic_color: #30E7,
logo_color: #30E7,
title_color: #494D50,
foot_tolor: #5E6165,
font_color1: #909399,
font_color2: #909399,
logo_image: url('@/assets/image/logo.png'),
big_logo_image: url('@/assets/image/logo_big.png'),
banner_image: url('@/assets/image/basic_banner.png'),
search_btn: linear-gradient(187deg, #5E9DF5 0%, #30E7 47%),
border_bottom_1: 1px solid #30E7,
card_border_1: 1px solid rgba(208,211,219,1),
zhuce_card: #fff,
font_hover: #30E7,
navbar_background: #fff,
background_color2: #f0f2f5,
zhuce_background: #fff,
hangye_background: #fff,
background_color3: red,
background_color4: #2674e7,
border_bottom: 5px solid #4554DE,
),
red: (
basic_color: #D0021B,
logo_color: #fff,
title_color: #494D50,
foot_tolor: #5E6165,
font_color1: #909399,
font_color2: #fff,
font_hover: #D0021B,
logo_image: url('@/assets/image/logo_white.png'),
big_logo_image: url('@/assets/theme/redTheme/logo-b.png'),
banner_image: url('@/assets/theme/redTheme/banner-bg.png'),
search_btn: linear-gradient(187deg, #d20000 0%, #da0707de 47%),
border_bottom_1: 2px solid #D0021B ,
card_border_1: 1px solid rgba(208,211,219,1),
zhuce_card: #fff,
navbar_background: url('@/assets/theme/basicTheme/top-bg.png'),
background_color2: #283142,
zhuce_background: #fff,
hangye_background: #fff,
background_color3: #1e6ceb,
background_color4: #323e4e,
border_bottom: 5px solid #fff,
),
blue: (
basic_color: #0DECFF ,
logo_color: #fff,
title_color: #fff,
foot_tolor: #B2D4F5 ,
font_color1: #909399,
font_color2: #fff,
font_hover: #0E458C,
logo_image: url('@/assets/image/logo_white.png'),
big_logo_image: url('@/assets/theme/redTheme/logo-b.png'),
banner_image: url('@/assets/theme/blueTheme/y.png'),
search_btn: linear-gradient(187deg, #0076FF 0%, #0076FF 47%),
border_bottom_1: 2px solid #0DECFF ,
zhuce_card: #034488,
card_border_1: 2px solid rgba(8,93,185,1),
navbar_background: #0E458C,
foot_background: linear-gradient(131deg, #005FBC 0%, #08215F 100%),
news_background: url('@/assets/theme/blueTheme/科技蓝.png') no-repeat,
zhuce_background: #033367,
hangye_background: #033367,
background_color2: #283142,
background_color3: #1e6ceb,
background_color4: #323e4e,
border_bottom: 5px solid #fff,
),
);
- src/style/下的 _handle.scss来操作上述1中的$theme变量(当然两个文件可以合并,分开写是想把配置和操作解耦),_handle.scss文件内容:
@import "./_theme.scss";
@mixin themeify {
@each $theme-name, $theme-map in $themes {
$theme-map: $theme-map !global;
[data-theme="#{$theme-name}"] & {
@content;
}
}
}
@function themed($key) {
@return map-get($theme-map, $key);
}
@mixin background_color($color) {
@include themeify {
background-color: themed($color)!important;
}
}
@mixin background($color) {
@include themeify {
background: themed($color)!important;
}
}
@mixin content($color) {
@include themeify {
content: themed($color)!important;
}
}
@mixin border_bottom($color) {
@include themeify {
border-bottom: themed($color)!important;
}
}
@mixin border($color) {
@include themeify {
border: themed($color)!important;
}
}
@mixin font_color($color) {
@include themeify {
color: themed($color)!important;
}
}
@mixin border_color($color) {
@include themeify {
border-color: themed($color)!important;
}
}
二、在组件中使用已经创建好的scss文件
<template>
<button @click="toggleButtonStyle">切换按钮样式</button>
<div :class="buttonClass">
显示的样式
</div>
</template>
<script>
export default {
data() {
return {
isStyle1:true
};
},
computed: {
buttonClass() {
return this.isStyle1? 'button-style-1 box' : 'button-style-2 box';
}
},
methods: {
toggleButtonStyle() {
this.isStyle1 =!this.isStyle1;
},
theme(type){
window.document.documentElement.setAttribute("data-theme",type)
}
},
created(){
this.theme("blue")
}
};
</script>
<style lang="scss">
@import './style/handle';
.button-style-1 {
@include background_color("navbar_background");
}
.button-style-2 {
@include background_color("background_color2");
}
.box{
height: 200px;
line-height: 200px;
text-align: center;
}
</style>
- 此处根据具体情况具体分析,在合适的页面或位置写入即可,此处我是放到了 App.vue项目入口文件中,在进入文件后 通过
window.document.documentElement.setAttribute()方法传入你当前想要使用的主题,此处我传入的’blue‘(蓝色主题),则vue页面中使用的即为_theme.scss中的 blue对象下配置好的颜色或者其他属性, - 为其他主题色(如’red’,‘basic’同理,前提是_theme.scss文件中有已经配置好的这两个主题色)