Less 基于参数存在性的条件混合

Less 基于参数存在性的条件混合,less,less-mixins,Less,Less Mixins,有没有关于如何基于参数存在创建条件mixin的建议? 例如,我需要验证是否传递了所有参数以执行某些操作,例如: .margin (@margintop:0,@marginbottom:0,@marginright:0,@marginleft:0) { // if @marginright:0 or @marginleft:0 are passed do that... // else... } 1: 通常,当您需要为传递的不同数量的参数生成不同的内容时,根本不需要使用默认参数值,

有没有关于如何基于参数存在创建条件mixin的建议? 例如,我需要验证是否传递了所有参数以执行某些操作,例如:

.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中的每一个都可以重用其他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.
}
三: 第三种方法是使用变量参数并测试它们的计数,但我想这是最冗长和最愚蠢的方法:

.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;
}

尽管在某些特殊的使用情况下,它可能有其优点。

谢谢方法2套件对我来说很好!