前面介绍了less的变量和extend语法,今天在研究下混合属性(Mixin)。混合能够说是less的另外一个特征,你能够将通用属性定义在一块,而后使用时直接调用此混合属性。web
在 LESS 中咱们能够定义一些通用的属性集为一个选择器,而后在另外一个选择器中去调用这些属性. 例如:less
.a, #b { color: red; } .mixin-class { .a(); } .mixin-id { #b(); }
编译后函数
.a, #b { color: red; } .mixin-class { color: red; } .mixin-id { color: red; }
注意:在调用混合时,能够加括号也能够不加括号。下面这个也是对的:spa
.a, #b { color: red; } .mixin-class { .a; } .mixin-id { #b; }
若是你只想定义一个混合,则能够再选择器后面加上括号,以下:rest
.my-mixin { color: black; } .my-other-mixin() { background: white; } .class { .my-mixin; .my-other-mixin; }
编译后,加括号的.my-other-mixin()不会被编译。code
.my-mixin { color: black; } .class { color: black; background: white; }
任何 CSS class, id 或者 元素 属性集均可以以一样的方式引入.通用选择器中能够嵌套选择器。blog
若是你想混合属性在一个更复杂的选择器,能够叠放多个id或类。以下:it
#outer { .inner { color: red; } }
若是想使用这个混合属性,你能够这样,下面四个都是等价的编译
.c{ #outer > .inner; } .c{ #outer > .inner(); } .c{ #outer.inner; } .c{ #outer.inner(); }
你能够将混合属性定义在一个id的下面,这样就避免了与其余混合冲突。class
!important:
在使用混合属性后面加上!important关键字,则混合中的全部属性都会加上关键字!important。例如:
.foo (@bg: #f5f5f5, @color: #900) { background: @bg; color: @color; } .unimportant { .foo(1); } .important { .foo(2) !important; }
编译后
.unimportant { background: #f5f5f5; color: #900; } .important { background: #f5f5f5 !important; color: #900 !important; }
混合属性也能够经过括号传递参数,以下:
.border-radius(@radius) { -webkit-border-radius: @radius; -moz-border-radius: @radius; border-radius: @radius; }
咱们只须要在使用它的时候传递一个参数便可,以下:
#header { .border-radius(4px); } .button { .border-radius(6px); }
固然咱们也能够给参数一个默认值,这样使用的时候能够传值也能够不传值。以下:
.border-radius(@radius: 5px) { -webkit-border-radius: @radius; -moz-border-radius: @radius; border-radius: @radius; }
若是咱们没有传值,则会使用默认值5px。
固然咱们也能够传递多个参数,以下:
.mixin(@color) { color-1: @color; } .mixin(@color; @padding:2) { color-2: @color; padding-2: @padding; } .mixin(@color; @padding; @margin: 2) { color-3: @color; padding-3: @padding; margin: @margin @margin @margin @margin; } .some .selector div { .mixin(#008000); }
编译后
.some .selector div { color-1: #008000; color-2: #008000; padding-2: 2; }
从编译的结果能够看出,less也有函数重载的特性。当咱们定义相同混合属性名,参数不一样,而后.mixin(#008000);调用,第一和第二混合都能匹配,可是第三个缺乏参数@padding的值,因此不会引用第三个混合属性。
咱们不只能够传多个值,还能够指定属性名传值,以下:
.mixin(@color: black; @margin: 10px; @padding: 20px) { color: @color; margin: @margin; padding: @padding; } .class1 { .mixin(@margin: 20px; @color: #33acfe); } .class2 { .mixin(#efca44; @padding: 40px); }
@arguments有特殊的含义,相似于js的arguments,他包含了传递给混合属性的全部参数,以下:
.box-shadow(@x: 0; @y: 0; @blur: 1px; @color: #000) { -webkit-box-shadow: @arguments; -moz-box-shadow: @arguments; box-shadow: @arguments; } .big-block { .box-shadow(2px; 5px); }
编译后
.big-block { -webkit-box-shadow: 2px 5px 1px #000; -moz-box-shadow: 2px 5px 1px #000; box-shadow: 2px 5px 1px #000; }
与@arguments不一样的是@reset包含除指明参数以外的参数,例如:
.mixin(@a; @rest...) { // @rest包含了@a以后的参数 // @arguments包含了全部参数 }
有时候你想让混合根据你传入的参数作不一样的事情,好比:
.mixin(dark; @color) { color: darken(@color, 10%); } .mixin(light; @color) { color: lighten(@color, 10%); } .mixin(@_; @color) { display: block; } .class { .mixin(@switch; #888); }
对于.class你赋给变量@switch不一样的值,不一样的混合属性会被调用,好比
@switch: light;
编译后
.class { color: #a2a2a2; display: block; }
当咱们把混合当作函数使用时,在调用函数以后,函数中的变量是可使用的,除非调用混合属性的元素本身定义了一样的变量。好比:
.mixin() { @width: 100%; @height: 200px; } .caller { .mixin(); width: @width; height: @height; }
编译后
.caller { width: 100%; height: 200px; }
.average(@x, @y) { @average: ((@x + @y) / 2); } div { .average(16px, 50px); // "call" the mixin padding: @average; // use its "return" value }
编译后
div { padding: 33px; }