gpt4 book ai didi

less - 基于参数存在的条件混合

转载 作者:行者123 更新时间:2023-12-02 08:37:09 24 4
gpt4 key购买 nike

关于如何根据参数存在创建条件混入有什么建议吗?例如,我需要验证是否传递了所有参数以便执行某些操作,例如:

.margin (@margintop:0,@marginbottom:0,@marginright:0,@marginleft:0) {

// if @marginright:0 or @marginleft:0 are passed do that...

// else...

}

最佳答案

1:

通常,当您需要为传递的不同数量的参数生成不同的东西时,您根本不需要使用默认参数值,例如:

.margin(@top, @bottom, @right, @left) {
/* right and left are passed */
}

.margin(@top, @bottom) {
/* right and left are not passed */
}

.margin() {
/* no arguments passed */
}

// etc.

请注意,这些 mixin 中的每一个都可以重用其他的,例如 .margin(@top, @bottom) 可以为“没有左右情况”做一些特殊的事情,然后调用 .margin(@top, @bottom, 0, 0) 执行主要工作。

2:

但是如果出于某种原因您仍然需要这些默认值,您可以使用一些不能作为有效边距的特殊默认值,例如像这样:

.margin(@top: undefined, @bottom: undefined, @right: undefined, @left: undefined) {
.test-args();

.test-args() when (@right = undefined) {
/* right is not passed */
}
.test-args() when (@left = undefined) {
/* left is not passed */
}

.test-args()
when not(@right = undefined)
and not(@left = undefined) {
/* right and left are passed */
}

// etc.
}

3:

第三种选择是使用可变参数并测试它们的数量,但我猜这是最冗长和最愚蠢的:

.margin(@args...) {
.eval-args(length(@args)); // requires LESS 1.5.+
.eval-args(@nargs) {
// default values:
@top: not passed;
@bottom: not passed;
@right: not passed;
@left: not passed;
}
.eval-args(@nargs) when (@nargs > 0) {
@top: extract(@args, 1);
}
.eval-args(@nargs) when (@nargs > 1) {
@bottom: extract(@args, 2);
}
.eval-args(@nargs) when (@nargs > 2) {
@right: extract(@args, 3);
}
.eval-args(@nargs) when (@nargs > 3) {
@left: extract(@args, 4);
}

args: @top, @bottom, @right, @left;
}

虽然它可能在某些特殊用例中有其优点。

关于less - 基于参数存在的条件混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20238472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com