sass mixin参数 - 检查模式

时间:2016-11-04 15:08:13

标签: sass mixins

如何检查传递给mixin的所有参数是否都是相同的值?

伪代码:

@mixin cols($width...) {
  @if (all $width values are the same) {
    // do something
  }
}

.three-cols {
  @include cols(4,4,4);
}

2 个答案:

答案 0 :(得分:0)

Sass支持andornot运算符作为布尔值。

你可以做到

@if ($width_1 == $width_2 and $width_2 == $width_3){
  // do something
}

更多信息:Using multiple conditions (AND) in SASS IF statement

答案 1 :(得分:0)

我委派检查的任务是查看值是否等于function,然后可以从mixin调用它们,以便它们都有单一责任。

@function check_values($values...) {
  $counter: 0;
  @if ( nth($values, 1) != nth($values,2) ) {
    $counter: 0;
  }
  @else {
    $counter: $counter + 1;
    @for $i from 2 to length($values) {
      $counter: if(nth($values, $i) == nth($values, $i + 1), $counter + 1, $counter );
    }
  }
  @return if($counter + 1 == length($values), true, false)
}

function返回 true false ,可用于任意数量的args

@debug check_values(2,2,1,1,2,2); //false
@debug check_values(2,2,2); //true
@debug check_values(2,2,2,1); //false

只需要在function

中调用mixin
@mixin cols($width...) {
  @if ( check_values($width...) ) {
    // do something
  }
}

希望这有帮助