简体   繁体   English

在mixin参数中使用较少

[英]LESS variable usage in mixin paramaters

I was hoping to create LESS mixin for border-radius that allows for two parameters to be set as defaults by two other parameters in the mixin declaration. 我希望为border-radius创建LESS mixin,允许两个参数在mixin声明中被另外两个参数设置为默认值。 The following is an example, that does not work, but resembles what I'm trying to achieve: 以下是一个示例,它不起作用,但类似于我想要实现的目标:

.border-radius-ext (@topleft: 0, @topright: 0, @bottomright: @topleft, @bottomleft: @topright) {
    border-top-left-radius: @topleft;
    border-top-right-radius: @topright;
    border-bottom-right-radius: @bottomright;
    border-bottom-left-radius: @bottomleft;
}

I realize I could have all the values set to 0 to begin with. 我意识到我可以将所有值设置为0开始。 That's not what I'm after. 那不是我追求的。 I was hoping that if the mixin were used like so: 我希望如果mixin这样使用:

blockquote {
    .border-radius-ext(3px, 5px);
}

The mixin would output: mixin将输出:

blockquote {
    border-top-left-radius: 3px;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 3px;
    border-bottom-left-radius: 5px;
}

...while still allowing the defaults for @bottomright and @bottomleft to be overridden if the mixin were used like so: ...如果使用mixin,仍然允许覆盖@bottomright@bottomleft的默认值:

blockquote {
    .border-radius-ext(3px, 5px, 7px, 2px);
}

In that instance, the output would be: 在那种情况下,输出将是:

blockquote {
    border-top-left-radius: 3px;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 7px;
    border-bottom-left-radius: 2px;
}

Is this not possible with LESS or am I just doing it wrong? 这是不可能的LESS或我只是做错了吗?

Default values for parameters can't be other parameters. 参数的默认值不能是其他参数。 But you can use this approach: 但是你可以使用这种方法:

.border-radius-ext (@topleft, @topright) {
    .border-radius-ext(@topleft, @topright, @topleft, @topright);
}

.border-radius-ext (@topleft, @topright, @bottomright, @bottomleft) {
    border-top-left-radius: @topleft;
    border-top-right-radius: @topright;
    border-bottom-right-radius: @bottomright;
    border-bottom-left-radius: @bottomleft;
}

Now you can use this mixin with either two or four parameters. 现在,您可以将此mixin与两个或四个参数一起使用。 You could also easily create a version with three parameters if needed. 如果需要,您还可以轻松创建包含三个参数的版本。

Because your mixin is expecting 4 variables - you need to input four. 因为你的mixin需要4个变量 - 你需要输入4个变量。 Setting default values will only work if you have defaults for all the variables and you pass in nothing - or (i think) - if you always put the variables at the beginning and the ones with the defaults at the end. 设置默认值只有在你拥有所有变量的默认值并且没有传递任何东西时才会起作用 - 或者(我认为) - 如果你总是将变量放在开头,而默认值放在最后。

In any case, I recommend just using four variables, possibly with defaults, because if another developer goes to work on your LESS it will be less confusing. 在任何情况下,我建议只使用四个变量,可能使用默认值,因为如果另一个开发人员在你的LESS上工作,那就不那么容易混淆了。

Something like this would be good: 这样的事情会很好:

.border-radius-ext (@topleft: 0, @topright: 0, @bottomright: 0, @bottomleft: 0) {
    border-top-left-radius: @topleft;
    border-top-right-radius: @topright;
    border-bottom-right-radius: @bottomright;
    border-bottom-left-radius: @bottomleft;
}

.border-radius-ext(3px, 5px, 7px, 2px);

I also recommend using LESS elements, http://lesselements.com/ , a great collection of pre-built mixins. 我还建议使用LESS元素, http://lesselements.com/ ,这是一个很好的预制mixin系列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM