逻辑类相关
约 222 字小于 1 分钟
2025-04-14
一、if()
根据条件返回两个值中的一个。
1、语法
if(condition, value1, value2)
condition
:一个布尔表达式。value1
:如果condition
为真时返回的值。value2
:如果condition
为假时返回的值。
注意
在 Less 3.6 版本之前,条件表达式必须使用括号包裹。
if(2 > 1, blue, green); // 在 3.0-3.5.3 版本会导致错误
if((2 > 1), blue, green); // 3.6+ 版本正常
2、例子
示例 1
@some: foo;
div {
margin: if((2 > 1), 0, 3px);
color: if((iscolor(@some)), @some, black);
}
编译后:
div {
margin: 0;
color: black;
}
示例 2
if(not (true), foo, bar);
if((true) and (2 > 1), foo, bar);
if((false) or (isstring("boo!")), foo, bar);
二、boolean()
返回 true 或 false
1、语法
boolean(condition)
参数condition
: 布尔表达式
2、示例
@bg: black;
@bg-light: boolean(luma(@bg) > 50%);
div {
background: @bg;
color: if(@bg-light, black, white);
}
编译后:
div {
background: black;
color: white;
}
更新日志
2025/8/24 08:17
查看所有更新日志
e7112
-1于