检查此函数
@function mathOperation($number, $type) {
@if $type == sum or $type == rest {
@return true
} @else {
@return false
}
}
@debug mathOperation(5, sum);
@debug mathOperation(5, rest);
@debug mathOperation(5, division);
正如预期的那样,结果是这样的
调试:真
调试:真
调试:假
但是如果我们将此函数中的相等运算符修改为 !=
@function mathOperation2($number, $type) {
@if $type != sum or $type != rest {
@return true
} @else {
@return false
}
}
@debug mathOperation2(5, sum);
@debug mathOperation2(5, rest);
@debug mathOperation2(5, division);
结果变得奇怪
调试:真
调试:真
调试:真
我错过了什么吗? 这种行为是一个错误吗?我正在使用咕噜咕噜 2.1.0 进行编译
对于第二个示例,代码应为:
@function mathOperation2($number, $type) {
@if $type != sum and $type != rest {
@return true
} @else {
@return false
}
}
if
语句中的or
表示,如果两个条件之一为真,则将接受该条件。您应该使用 and
,因此如果两个语句都为 true,则条件将为 true。
非常感谢你的回答,我最近5天没有睡觉(儿子在医院( 我用地图解决了它:
@function map-select-key($map, $key, $direction) {
$direction-map: (previous: -1, next: 1);
@if map-has-key($direction-map, $direction) == false {
@warn '#{$direction} is not an accepted parameter, the only
parameters accepted for $direction are "#{map-keys($direction-map)}"';
@return null;
} @else if (map-index-from-key($map, $key) == 1) and ($direction == previous) or (map-index-from-key($map, $key) == length($map)) and ($direction == next) {
@warn 'it is not possible to select the #{$direction} item: #{$key} is the #{if($direction == previous, first, last)} element on the map #{$map}';
@return null;
} @else {
@return map-key-from-index($map, map-index-from-key($map, $key) + map-get($direction-map, $direction));
}
}