zoukankan      html  css  js  c++  java
  • 大家一起学习less 1:混合

    我们先看以下一段例子:

    // LESS
    
    .rounded-corners (@radius: 5px) {
      border-radius: @radius;
      -webkit-border-radius: @radius;
      -moz-border-radius: @radius;
    }
    
    #header {
      .rounded-corners;
    }
    #footer {
      .rounded-corners(10px);
    }
    
    	
    
    /* 生成的 CSS */
    
    #header {
      border-radius: 5px;
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
    }
    #footer {
      border-radius: 10px;
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
    }
    

    最上方,其实相当于定义了一个名为“.rounded-corners ”的函数,它有一个参数叫@radius,默认值为5px。然后对于接着下来的样式规则,它先判定其是否符合CSS语法不,符合直接输出跳过,不符合就当成函数。

    有了变量与函数,css就可以实现更大规模的重用了!

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

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

    引导

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

    为了尽可能地保留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) {  @a }
    .max (@a, @b) when (@a < @b) {  @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) { ... }
    
    
查看全文
  • 相关阅读:
    《人月神话》读后感第一篇
    MD5+Salt值
    java第十三周测试记录
    今天的问题上上周考试也遇到了,解决方案在文章中。
    Jsp俩大内置对象学习
    十二周周四学术交流会报告
    web界面直连MySql数据库
    抽象类的知识
    isinstance与type的区别
    三级菜单
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/2683445.html
  • Copyright © 2011-2022 走看看