Less 自定义函数中使用 if else 判断
在 Less 自定义函数中进行 if else 判断可以使用三元运算符。以下是一个示例:
.my-function(@value) {
@result: if(@value > 0, 'positive', 'non-positive');
// 使用三元运算符进行判断
@result: @value > 0 ? 'positive' : 'non-positive';
// 输出结果
.result {
value: @result;
}
}
在上面的示例中,.my-function 函数接受一个参数 @value。使用 if 函数或三元运算符 ? :,根据 @value 的值判断 @result 的值是 'positive' 还是 'non-positive'。然后,将 @result 的值输出为 .result 选择器的一个属性。
可以使用类似的方法进行更复杂的判断,例如嵌套的 if else 语句。
.my-function(@value) {
@result: if(@value > 0, 'positive', if(@value < 0, 'negative', 'zero'));
// 输出结果
.result {
value: @result;
}
}
在这个示例中,根据 @value 的值,@result 的值可能是 'positive'、'negative' 或 'zero'。这是通过嵌套的 if else 语句实现的。
原文地址: https://www.cveoy.top/t/topic/qhEF 著作权归作者所有。请勿转载和采集!