美文网首页LESS
Less用法之——模式匹配和引导表达式

Less用法之——模式匹配和引导表达式

作者: 杀个程序猿祭天 | 来源:发表于2018-11-14 11:55 被阅读18次

    模式匹配和导引表达式

    有些情况下,我们想根据传入的参数来改变混合的默认呈现,比如下面这个例子:

    .mixin (@s, @color) { ... }
    
    .class {
      .mixin(@switch, #888);
    }
    

    如果想让.mixin根据不同的@switch值而表现各异,如下这般设置:

    .mixin (dark, @color) {
      color: darken(@color, 10%);
    }
    .mixin (light, @color) {
      color: lighten(@color, 10%);
    }
    .mixin (@_, @color) {
      display: block;
    }
    

    现在,如果运行:

    @switch: light;
    
    .class {
      .mixin(@switch, #888);
    }
    

    就会得到下列CSS:

    .class {
      color: #a2a2a2;
      display: block;
    }
    

    如上,.mixin就会得到传入颜色的浅色。如果@switch设为dark,就会得到深色。

    具体实现如下:
    
    第一个混合定义并未被匹配,因为它只接受dark做为首参
    第二个混合定义被成功匹配,因为它只接受light
    第三个混合定义被成功匹配,因为它接受任意值
    只有被匹配的混合才会被使用。变量可以匹配任意的传入值,而变量以外的固定值就仅仅匹配与其相等的传入值。
    
    我们也可以匹配多个参数:
    
    .mixin (@a) {
      color: @a;
    }
    .mixin (@a, @b) {
      color: fade(@a, @b);
    }
    Now if we call .mixin with a single argument, we will get the output of the first definition, but if we call it with two arguments, we will get the second definition, namely @a faded to @b.
    

    引导

    当我们想根据表达式进行匹配,而非根据值和参数匹配时,导引就显得非常有用。如果你对函数式编程非常熟悉,那么你很可能已经使用过导引。

    为了尽可能地保留CSS的可声明性,LESS通过导引混合而非if/else语句来实现条件判断,因为前者已在@media query特性中被定义。

    以此例做为开始:

    .mixin (@a) when (lightness(@a) >= 50%) {
      background-color: black;
    }
    .mixin (@a) when (lightness(@a) < 50%) {
      background-color: white;
    }
    .mixin (@a) {
      color: @a;
    }
    

    when关键字用以定义一个导引序列(此例只有一个导引)。接下来我们运行下列代码:

    .class1 { .mixin(#ddd) }
    .class2 { .mixin(#555) }
    

    就会得到:

    .class1 {
      background-color: black;
      color: #ddd;
    }
    .class2 {
      background-color: white;
      color: #555;
    }
    

    导引中可用的全部比较运算有: > >= = =< <。此外,关键字true只表示布尔真值,下面两个混合是相同的:

    .truth (@a) when (@a) { ... }
    .truth (@a) when (@a = true) { ... }
    除去关键字true以外的值都被视示布尔假:
    
    .class {
      .truth(40); // Will not match any of the above definitions.
    }
    

    导引序列使用逗号‘,’—分割,当且仅当所有条件都符合时,才会被视为匹配成功。

    .mixin (@a) when (@a > 10), (@a < -10) { ... }
    导引可以无参数,也可以对参数进行比较运算:
    
    @media: mobile;
    
    .mixin (@a) when (@media = mobile) { ... }
    .mixin (@a) when (@media = desktop) { ... }
    
    .max (@a, @b) when (@a > @b) { width: @a }
    .max (@a, @b) when (@a < @b) { width: @b }
    最后,如果想基于值的类型进行匹配,我们就可以使用is*函式:
    
    .mixin (@a, @b: 0) when (isnumber(@b)) { ... }
    .mixin (@a, @b: black) when (iscolor(@b)) { ... }
    

    下面就是常见的检测函式:

    iscolor
    isnumber
    isstring
    iskeyword
    isurl
    如果你想判断一个值是纯数字,还是某个单位量,可以使用下列函式:
    
    ispixel
    ispercentage
    isem
    

    最后再补充一点,在导引序列中可以使用and关键字实现与条件:

    .mixin (@a) when (isnumber(@a)) and (@a > 0) { ... }
    

    使用not关键字实现或条件

    .mixin (@b) when not (@b > 0) { ... }
    

    相关文章

      网友评论

        本文标题:Less用法之——模式匹配和引导表达式

        本文链接:https://www.haomeiwen.com/subject/mtrlfqtx.html