如何检查传递给mixin的所有参数是否都是相同的值?
伪代码:
@mixin cols($width...) {
@if (all $width values are the same) {
// do something
}
}
.three-cols {
@include cols(4,4,4);
}
答案 0 :(得分:0)
Sass支持and
,or
和not
运算符作为布尔值。
你可以做到
@if ($width_1 == $width_2 and $width_2 == $width_3){
// do something
}
答案 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
}
}
希望这有帮助