Less mixin and variables

无人久伴 提交于 2019-12-02 05:47:12

问题


I have the following mixin:

.iconFont(@color: @green, @font-size: 18px){
  color: @color;
  font-size: @font-size;
}

If I want only to change the second variable value, I need to write the first variable default value?

h1{
 .iconFont(@green, 14px);
}

回答1:


No, there is no need to specify the default value for the first parameter while calling the function. Instead you can just use named parameters feature to explicitly let the compiler know that the value you are passing in the mixin call is for the 2nd parameter.

.sample{
    .iconFont(@font-size:14px);
}

The above Less code when compiled would produce the below output. (Note: I had set the @green as #00ff00.)

.sample {
    color: #00ff00;
    font-size: 14px;
}

While using the named parameter feature, even the order in which the parameters are passed does not matter. For example, the same mixin can be called as follows:

.sample2{
    .iconFont(@font-size:24px, @color: #070707);
}

And it would produce the below as output.

.sample2 {
    color: #070707;
    font-size: 24px;
}


来源:https://stackoverflow.com/questions/25787882/less-mixin-and-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!