Vue中使用sass实现换肤功能

先给大家展示下效果图:

 

Vue中使用sass实现换肤功能

先给大家看一下目录和主要文件:

Vue中使用sass实现换肤功能

解释一下主要文件:

base.scss: 一些通用样式文件。

mixin.scss: 定义mixin方法的文件。

varibale.scss: 颜色,字体,背景的配置文件

以下就拿封装的head组件代码来展示以下实现逻辑,现在大家主要是来理解,不要着急复制代码,在文章最后会贴出三个主要文件的代码的。

Vue中使用sass实现换肤功能

为什么会在 background:$background-color-theme; 地方标注错误?

如果之前用过sass的同学可能会知道,这样虽然实现了css样式变量化,但是后期没有办法作出灵活更改的。

所以需要把设置背景颜色封装成一个mixin方法(包括字体大小,字体颜色,都需要进行封装)

请看mixin.scss中的代码:

Vue中使用sass实现换肤功能

主要原理:

通过设置html的attribute属性在封装的函数中进行判断,进行相应的设置不同的颜色

css中 [ ] 可以识别到在html标签上设置的属性,所以在html上对应属性发生变化时,就会执行相应的样式,

这一步有点类似于平时给div添加一个.active属性,css自动执行相应样式。

更换主题时的具体操作:

Vue中使用sass实现换肤功能

下边是主要文件完整的代码

base.scss示例代码:

@charset "utf-8"; $font_default_color:$font-color-shallow3; $font_default_size:$font_medium_s; *{ margin:0;padding:0;box-sizing:border-box;color:$font_default_color; /*@include font-dpr($font_default_size);*/ } a{text-decoration:none;color:$font_default_color;} .sub-page { //routerView position: fixed;top: 0;bottom: 0;width: 100%;background:#fff;right: 0;left: 0;z-index: 5; } #content{width:100%;position:absolute;@include px2rem(top,88px);bottom:0;overflow-x:auto;} .width{ width:100%; } /*竖向居中*/ .table-cell{ display:table-cell;vertical-align:middle;text-align:center; } .middle{ vertical-align:middle; } /*弹性盒*/ .flex{ display: inline-flex;display: -webkit-flex;display: flex; } /*弹性盒-子元素可竖向居中*/ .flex-middle{ display :flex; display:-webkit-flex; align-items:center; -webkit-align-items:center; justify-content:center ; } .tl{ text-align:left; } .tc{ text-align:center; } .tr{ text-align:right; } .fl{ float:left; } .fr{ float:right; } .clear::after{ /*原理: overflow!=visible ;display!=block;float!=none;position!=static||relative 都可为元素创建BFC;消除边距重叠或者浮动产生的影响*/ content:'';overflow:hidden;clear:both; }

mixin.scss示例代码:

@charset "utf-8"; @import "./variable";/*引入配置*/ @mixin font_size($size){/*通过该函数设置字体大小,后期方便统一管理;*/ @include font-dpr($size); } @mixin font_color($color){/*通过该函数设置字体颜色,后期方便统一管理;*/ color:$color; [data-theme="theme1"] & { color:$font-color-theme1; } [data-theme="theme2"] & { color:$font-color-theme2; } [data-theme="theme3"] & { color:$font-color-theme3; } } @mixin bg_color($color){/*通过该函数设置主题颜色,后期方便统一管理;*/ background-color:$color; [data-theme="theme1"] & { background-color:$background-color-theme1; } [data-theme="theme2"] & { background-color:$background-color-theme2; } [data-theme="theme3"] & { background-color:$background-color-theme3; } } /*px转rem*/ @mixin px2rem($property,$px,$px2:false,$px3:false,$px4:false){ $rem:75px;/* 设计稿尺寸/10 */ @if $px and $px2 and $px3 and $px4 { #{$property}: ($px / $rem) + rem ($px2 / $rem) + rem ($px3 / $rem) + rem ($px4 / $rem) + rem; } @else if $px and $px2 { #{$property}: ($px / $rem) + rem ($px2 / $rem) + rem; //[data-model='pad'] & {#{$property}: ($px * 1.4 / $rem) + rem ($px2 * 1.4 / $rem) + rem;} } @else{ #{$property}: ($px / $rem) + rem!important; //[data-model='pad'] & {#{$property}: ($px * 1.4 / $rem) + rem;} } } /*根据dpr计算font-size*/ @mixin font-dpr($font-size){ font-size: $font-size; //[data-model="pad"] & { font-size: $font-size * 1.4; } [data-dpr="2"] & { font-size: $font-size * 2;} [data-dpr="3"] & { font-size: $font-size * 3;} } /*弹性盒属性*/ %flexbox{ display: inline-flex;display: -webkit-flex;display: flex; } /*弹性盒比例*/ @mixin flex($num:1){ -webkit-box-flex:$num;-moz-box-flex:$num;-webkit-flex:$num;-ms-flex:$num;flex:$num; } /*超行溢出显示省略号*/ @mixin overflow($num:1,$fontSize:0,$lineHeight:1.5){ display: -webkit-box;-webkit-line-clamp:$num; overflow: hidden; /*! autoprefixer: off */ -webkit-box-orient: vertical; /* autoprefixer: on */ @if $fontSize!=0 and $lineHeight{/*高度需要撑开*/ line-height:$lineHeight; @if $lineHeight < 1.2 { line-height:1.2; /*最小需要1.2,否则在部分安卓机下第$num+1行会顶部漏出*/ } height: $num * $fontSize * $lineHeight; [data-dpr="2"] & { height: $num * $fontSize * $lineHeight * 2!important;} [data-dpr="3"] & { height: $num * $fontSize * $lineHeight * 3!important;} } } //transition兼容写法 @mixin transition($content:all .2s){ -moz-transition: $content; -webkit-transition: $content; -o-transition: $content; transition: $content; } //transfrom兼容 @mixin translateX($num:-50%){ -ms-transform: translateX($num); -moz-transform: translateX($num); -webkit-transform: translateX($num); -o-transform: translateX($num); transform: translateX($num); } @mixin translateY($num:-50%){ -ms-transform: translateY($num); -moz-transform: translateY($num); -webkit-transform: translateY($num); -o-transform: translateY($num); transform: translateY($num); } @mixin rotate($deg:90deg){ -ms-transform:rotate($deg); -moz-transform:rotate($deg); -webkit-transform:rotate($deg); -o-transform:rotate($deg); transform:rotate($deg); }

variable.scss示例代码:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5803a2d74b5ac32f4da3d070b010f15f.html