sass传参

sass传参用@mixin来声明,用@include来调用;例如:其中@mixin opa($opa:50)中的$opa:50表示:不传参数时opa=50,否则为所传参数。

@mixin opa($opa:50) {  
	opacity:$opa/100;
	filter:alpha(opacity=$opa);
}
.demo{
	@include opa(30);
	width:$width;
	height:$width;
	margin:0 auto;
	text-align:center;
	color:red;
	font-size:20px;
	background-color:blue;
}

如果@mixin opa(),此时可以不传参数

@mixin opa() {  	
        opacity:0.5;
	filter:alpha(opacity=50);
}
.demo{
	@include opa();
	width:$width;
	height:$width;
	margin:0 auto;
	text-align:center;
	color:red;
	font-size:20px;
	background-color:blue;
}

 如果@mixin opa($opa),则必须传参,否则用ruby的sass编译不通过。

当sass传多个参数时用逗号隔开, 也可以跳过中间参数,直接给最后一个传参,但要加上变量名:

例如:给三角形传参@include sj($fx:left,$color:orange)就跳过中间的$size直接给$color传参。

@mixin sj($fx:top,$size:200px,$color:red){
	@if ($fx==top){
		border-style:dashed dashed solid dashed;
		border-color:transparent transparent $color transparent;
	}
	@if ($fx==right){
		border-style:dashed dashed dashed solid;
		border-color:transparent transparent transparent $color;
	}
	@if ($fx==bottom){
		border-style:solid dashed dashed dashed;
		border-color:$color transparent transparent transparent;
	}
	@if ($fx==left){
		border-style:dashed solid dashed dashed;
		border-color:transparent $color transparent transparent;
	}
	width: 0;
	height: 0;
	overflow:hidden;
	border-width:$size;
}
.demo{
	@include sj($fx:left,$color:orange)
}

用sass编译后生成的css样式为:由于没有给$size传参,所以默认值200px

.demo {
  border-style: dashed solid dashed dashed;
  border-color: transparent orange transparent transparent;
  width: 0;
  height: 0;
  overflow: hidden;
  border-width: 200px; }

/*# sourceMappingURL=chuancan.css.map */

用浏览器打开,三角形形状如下图:

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值