您的位置:首页 > Web前端 > CSS

快速学sass(六)--混合宏

2017-03-13 22:36 169 查看

声明混合宏

如果你的整个网站中有几处小样式类似,比如颜色,字体等,在 Sass 可以使用变量来统一处理,那么这种选择还是不错的。但当你的样式变得越来越复杂,需要重复使用大段的样式时,使用变量就无法达到我们目了。这个时候 Sass 中的混合宏就会变得非常有意义。在这一节中,主要向大家介绍 Sass 的混合宏。

1、声明混合宏

不带参数混合宏:

在 Sass 中,使用“@mixin”来声明一个混合宏。如:

@mixin border-radius{
-webkit-border-radius: 5px;
border-radius: 5px;
}


其中 @mixin 是用来声明混合宏的关键词,有点类似 CSS 中的 @media、@font-face 一样。border-radius 是混合宏的名称。大括号里面是复用的样式代码。

带参数混合宏:

除了声明一个不带参数的混合宏之外,还可以在定义混合宏时带有参数,如:

@mixin border-radius($radius:5px){
-webkit-border-radius: $radius;
border-radius: $radius;
}


复杂的混合宏:

上面是一个简单的定义混合宏的方法,当然, Sass 中的混合宏还提供更为复杂的,你可以在大括号里面写上带有逻辑关系,帮助更好的做你想做的事情,如:

@mixin box-shadow($shadow...) {
@if length($shadow) >= 1 {
@include prefixer(box-shadow, $shadow);
} @else{
$shadow:0 0 4px rgba(0,0,0,.3);
@include prefixer(box-shadow, $shadow);
}
}


这个 box-shadow 的混合宏,带有多个参数,这个时候可以使用“ … ”来替代。简单的解释一下,当 $shadow 的参数数量值大于或等于“ 1 ”时,表示有多个阴影值,反之调用默认的参数值“ 0 0 4px rgba(0,0,0,.3) ”。

调用混合宏

在 Sass 中通过 @mixin 关键词声明了一个混合宏,那么在实际调用中,其匹配了一个关键词“@include”来调用声明好的混合宏。例如在你的样式中定义了一个圆角的混合宏“border-radius”:

@mixin border-radius{
-webkit-border-radius: 3px;
border-radius: 3px;
}


在一个按钮中要调用定义好的混合宏“border-radius”,可以这样使用:

button {
@include border-radius;
}


这个时候编译出来的 CSS:

button {
-webkit-border-radius: 3px;
border-radius: 3px;
}


混合宏的参数–传一个不带值的参数

Sass 的混合宏有一个强大的功能,可以传参,那么在 Sass 中传参主要有以下几种情形:

A) 传一个不带值的参数

在混合宏中,可以传一个不带任何值的参数,比如:

@mixin border-radius($radius){
-webkit-border-radius: $radius;
border-radius: $radius;
}


在混合宏“border-radius”中定义了一个不带任何值的参数“$radius”。

在调用的时候可以给这个混合宏传一个参数值:

.box {
@include border-radius(3px);
}


这里表示给混合宏传递了一个“border-radius”的值为“3px”。

编译出来的 CSS:

.box {
-webkit-border-radius: 3px;
border-radius: 3px;
}


混合宏的参数–传一个带值的参数

在 Sass 的混合宏中,还可以给混合宏的参数传一个默认值,例如:

@mixin border-radius($radius:3px){
-webkit-border-radius: $radius;
border-radius: $radius;
}


在混合宏“border-radius”传了一个参数“$radius”,而且给这个参数赋予了一个默认值“3px”。

在调用类似这样的混合宏时,会多有一个机会,假设你的页面中的圆角很多地方都是“3px”的圆角,那么这个时候只需要调用默认的混合宏“border-radius”:

.btn {
@include border-radius;
}


编译出来的 CSS:

.btn {
-webkit-border-radius: 3px;
border-radius: 3px;
}


但有的时候,页面中有些元素的圆角值不一样,那么可以随机给混合宏传值,如:

.box {
@include border-radius(50%);
}


编译出来的 CSS:

.box {
-webkit-border-radius: 50%;
border-radius: 50%;
}


混合宏的参数–传多个参数

Sass 混合宏除了能传一个参数之外,还可以传多个参数,如:

@mixin center($width,$height){
width: $width;
height: $height;
position: absolute;
top: 50%;
left: 50%;
margin-top: -($height) / 2;
margin-left: -($width) / 2;
}


在混合宏“center”就传了多个参数。在实际调用和其调用其他混合宏是一样的:

.box-center {
@include center(500px,300px);
}


编译出来 CSS:

.box-center {
width: 500px;
height: 300px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -150px;
margin-left: -250px;
}


有一个特别的参数“…”。当混合宏传的参数过多之时,可以使用参数来替代,如:

@mixin box-shadow($shadows...){
@if length($shadows) >= 1 {
-webkit-box-shadow: $shadows;
box-shadow: $shadows;
} @else {
$shadows: 0 0 2px rgba(#000,.25);
-webkit-box-shadow: $shadow;
box-shadow: $shadow;
}
}


在实际调用中:

.box {
@include box-shadow(0 0 1px rgba(#000,.5),0 0 2px rgba(#000,.2));
}


编译出来的CSS:

.box {
-webkit-box-shadow: 0 0 1px rgba(0, 0, 0, 0.5), 0 0 2px rgba(0, 0, 0, 0.2);
box-shadow: 0 0 1px rgba(0, 0, 0, 0.5), 0 0 2px rgba(0, 0, 0, 0.2);
}


混合宏的不足

混合宏在实际编码中给我们带来很多方便之处,特别是对于复用重复代码块。但其最大的不足之处是会生成冗余的代码块。比如在不同的地方调用一个相同的混合宏时。如:

@mixin border-radius{
-webkit-border-radius: 3px;
border-radius: 3px;
}

.box {
@include border-radius;
margin-bottom: 5px;
}

.btn { @include border-radius; }


示例在“.box”和“.btn”中都调用了定义好的“border-radius”混合宏。先来看编译出来的 CSS:

.box {
-webkit-border-radius: 3px;
border-radius: 3px;
margin-bottom: 5px;
}

.btn { -webkit-border-radius: 3px; border-radius: 3px; }


上例明显可以看出,Sass 在调用相同的混合宏时,并不能智能的将相同的样式代码块合并在一起。这也是 Sass 的混合宏最不足之处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  css sass