我写的mixin是这样的:

@mixin box-shadow($top, $left, $blur, $color, $inset:"") {
    -webkit-box-shadow: $top $left $blur $color $inset;
    -moz-box-shadow: $top $left $blur $color $inset;
    box-shadow: $top $left $blur $color $inset;
}

当调用时,我真正想要的是,如果没有$inset值被传递,什么都不输出,而不是编译成这样:

-webkit-box-shadow: 2px 2px 5px #555555 "";
-moz-box-shadow: 2px 2px 5px #555555 "";
box-shadow: 2px 2px 5px #555555 "";

我如何重写mixin,以便如果没有$inset传递的值,没有输出?


当前回答

更干燥的方式!

@mixin box-shadow($args...) {
  @each $pre in -webkit-, -moz-, -ms-, -o- {
    #{$pre + box-shadow}: $args;
  } 
  #{box-shadow}: #{$args};
}

现在你可以更聪明地重用你的盒影:

.myShadow {
  @include box-shadow(2px 2px 5px #555, inset 0 0 0);
}

其他回答

sass@3.4.9:

// declare
@mixin button( $bgcolor:blue ){
    background:$bgcolor;
}

而使用时没有价值,按钮会呈蓝色

//use
.my_button{
    @include button();
}

带有value, button会是红色的

//use
.my_button{
    @include button( red );
}

对六烷也有效

Sass支持@if语句。(请参考文档)

你可以这样写你的mixin:

@mixin box-shadow($top, $left, $blur, $color, $inset:"") {
  @if $inset != "" {
    -webkit-box-shadow:$top $left $blur $color $inset;
    -moz-box-shadow:$top $left $blur $color $inset;
    box-shadow:$top $left $blur $color $inset;
  }
}

更干燥的方式!

@mixin box-shadow($args...) {
  @each $pre in -webkit-, -moz-, -ms-, -o- {
    #{$pre + box-shadow}: $args;
  } 
  #{box-shadow}: #{$args};
}

现在你可以更聪明地重用你的盒影:

.myShadow {
  @include box-shadow(2px 2px 5px #555, inset 0 0 0);
}

超级简单的方法

只需添加一个默认值none到$inset - so

@mixin box-shadow($top, $left, $blur, $color, $inset: none) { ....

现在,当没有$inset被传递时,什么也不会显示。

以下是我使用的解决方案,附注如下:

@mixin transition($trans...) {
  @if length($trans) < 1 {
    $trans: all .15s ease-out;
  }
  -moz-transition: $trans;
  -ms-transition: $trans;
  -webkit-transition: $trans;
  transition: $trans;
}

.use-this {
  @include transition;
}

.use-this-2 {
  @include transition(all 1s ease-out, color 2s ease-in);
}

更喜欢将属性值作为原生CSS传递,以接近“舌头” 允许传递可变数量的参数 为惰性定义一个默认值