LESS变量作为参数混合

时间:2013-12-05 20:12:51

标签: css variables less mixins opentype

我对LESS比较新,我正试图找到一种方法来获得激活和停用OpenType功能的简单代码。我理解变量和参数混合是什么,但如果可能的话,我不确定如何将它们放在一起。
其他关于如何解决这个问题的建议将不胜感激。

/*Variables for enabling and disabling feature*/
@on: 1;
@off: 0;

/*Turn on feature*/
.liga (@on){
    -moz-font-feature-settings:"liga" 1; 
    -ms-font-feature-settings:"liga" 1; 
    -o-font-feature-settings:"liga" 1; 
    -webkit-font-feature-settings:"liga" 1; 
    font-feature-settings:"liga" 1;
}

/*Turn off feature*/
.liga (@off){
    -moz-font-feature-settings:"liga" 0; 
    -ms-font-feature-settings:"liga" 0; 
    -o-font-feature-settings:"liga" 0; 
    -webkit-font-feature-settings:"liga" 0; 
    font-feature-settings:"liga" 0;
}

/*Sample use*/
p{
    .liga(@on);
}

2 个答案:

答案 0 :(得分:3)

您可以使用when条件,不像这样:

@onoff:1;
.liga(@onoff) when (@onoff = 1) {
    -moz-font-feature-settings:"liga" 1; 
    -ms-font-feature-settings:"liga" 1; 
    -o-font-feature-settings:"liga" 1; 
    -webkit-font-feature-settings:"liga" 1; 
    font-feature-settings:"liga" 1;
}

.liga(@onoff) when (@onoff = 0) {
    -moz-font-feature-settings:"liga" 0; 
    -ms-font-feature-settings:"liga" 0; 
    -o-font-feature-settings:"liga" 0; 
    -webkit-font-feature-settings:"liga" 0; 
    font-feature-settings:"liga" 0;
}


p{
    .liga(@onoff);
}

或者您可以尝试创建一个唯一的函数并在其中传递值,如下所示:

.liga(@value){
        -moz-font-feature-settings:"liga" @value; 
        -ms-font-feature-settings:"liga" @value; 
        -o-font-feature-settings:"liga" @value; 
        -webkit-font-feature-settings:"liga" @value; 
        font-feature-settings:"liga" @value;
    }

p{
    .liga(1); //or .liga(0); or .liga(@on); or .liga(@off);
}

答案 1 :(得分:1)

您可以通过将参数mixins的参数替换为when1的实际预期值来避免0条件。

/*Variables for enabling and disabling feature*/
@on: 1;
@off: 0;

/*Turn on feature*/
.liga (1){ /*<-------CHANGED HERE */
    -moz-font-feature-settings:"liga" 1; 
    -ms-font-feature-settings:"liga" 1; 
    -o-font-feature-settings:"liga" 1; 
    -webkit-font-feature-settings:"liga" 1; 
    font-feature-settings:"liga" 1;
}

/*Turn off feature*/
.liga (0){ /*<-------CHANGED HERE */
    -moz-font-feature-settings:"liga" 0; 
    -ms-font-feature-settings:"liga" 0; 
    -o-font-feature-settings:"liga" 0; 
    -webkit-font-feature-settings:"liga" 0; 
    font-feature-settings:"liga" 0;
}

/*Sample use*/
p{
    .liga(@on);
}