动态的 css——less

less 是一种样式语言,它将 css 赋予了动态语言的特性,如变量、 继承、 运算、 函数。less 既能够在客户端上运行(支持 ie6+,webkit,firefox),也能够借助 Node.js 或者 Rhino 在服务端运行。javascript

less 作为 css 的一种形式的扩展,它并无阉割 css 的功能,而是在现有的 css 语法上,添加了不少额外的功能,因此对于前端开发人员来所,学习 less 是一件垂手可得的事情。咱们先看下用 less 写的一段 css:css

@base: #f938ab;
.box-shadow(@style, @c) when (iscolor(@c)) {
    box-shadow:         @style @c;
    -webkit-box-shadow: @style @c;
    -moz-box-shadow:    @style @c;
}
.box-shadow(@style, @alpha: 50%) when (isnumber(@alpha)) {
    .box-shadow(@style, rgba(0, 0, 0, @alpha));
}
.box { 
    color: saturate(@base, 5%);
    border-color: lighten(@base, 30%);
    div { .box-shadow(0 0 5px, 30%) }
}

在没有学过 less 的状况下,咱们并不知道这些代码是作啥用的,怎么生成咱们所熟悉的 css 代码,以上代码通过 less 编译后:html

.box {

    color: #fe33ac;

    border-color: #fdcdea;

}

.box div {

    box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);

    -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);

    -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);

}

下面咱们就一块儿来学习 less 吧。前端

咱们知道若是要使用 jquery 就必须在页面上引进 jquery 库,一样的在使用 less 编写 css 代码时,也要引进 less 库——less.js。点击这里下载 less 库。java

下载好后只要在页面中引入就能够了。jquery

<link rel="stylesheet/less" type="text/css" href="style.less" media="all" />

<script type="text/javascript" src="less.js"></script>

要注意外部引进样式的方法有所改变,rel 属性值为 stylesheet/less,样式的后缀变为 .less 同时 less 样式文件必定要在 less.js 前先引入。web

引入了 less 以后,正式开始学习 less。编程

less 语法less

一、变量编程语言

less 的变量充许你在样式中对经常使用的属性值进行定义,而后应用到样式中,这样只要改变变量的值就能够改变全局的效果。和 javascript 中的全局变量有点相似。

甚至能够用变量名定义为变量。

@color: red;
@foot: 'color';
.head{
    color: @color;
}
.foot{
    color: @@foot;
}

输出:

.head {

  color: red;

}

.foot {

  color: red;

}

注意 less 中的变量为彻底的“常量”,因此只能定义一次。

二、混合

混合就是定义一个 class,而后在其余 class 中调用这个 class。

.common{
    color: red;
}
.nav{
    background: #ccc;
    .common;
}

输出:

.common {
  color: red;
}
.nav {
  background: #ccc;
  color: red;
}

Css 中的 class, id 或者元素属性集均可以用一样的方式引入。

三、带参数混合

在 less 中,你能够把 class 当作是函数,而函数是能够带参数的。

.border-radius (@radius) {
    border-radius: @radius;
    -moz-border-radius: @radius;
    -webkit-border-radius: @radius;
}
#header {
    .border-radius(4px);
}
.button {
    .border-radius(6px);  
}

最后输出:

#header {

    border-radius: 4px;

    -moz-border-radius: 4px;

    -webkit-border-radius: 4px;

}

.button {

    border-radius: 6px;

    -moz-border-radius: 6px;

    -webkit-border-radius: 6px;

}

咱们还能够给参数设置默认值:

.border-radius (@radius: 5px) {
    border-radius: @radius;
    -moz-border-radius: @radius;
    -webkit-border-radius: @radius;
}
#header {
    .border-radius;  
}

最后输出:

#header {
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
}

也能够定义不带参数属性集合,若是想要隐藏这个属性集合,不让它暴露到CSS中去,可是还想在其余的属性集合中引用,就会发现这个方法很是的好用:

.wrap () {
    text-wrap: wrap;
    white-space: pre-wrap;
    white-space: -moz-pre-wrap;
    word-wrap: break-word;
}
pre {
    .wrap 
}

输出:

pre {

    text-wrap: wrap;

    white-space: pre-wrap;

    white-space: -moz-pre-wrap;

    word-wrap: break-word;

}

混合还有个重要的变量@arguments。

@arguments 包含了全部传递进来的参数,当你不想处理个别的参数时,这个将颇有用。

.border(@width:0,@style:solid,@color:red){
    border:@arguments;
}
.demo{
    .border(2px);
}

输出:

.demo {

    border: 2px solid #ff0000;

}

混合还有许多高级的应用,如模式匹配等。在这里就不介绍了,只讲些基础的东西。

四、嵌套规则

less 可让咱们用嵌套的方式来写 css。下面是咱们平时写的 css:

#header h1 {

    font-size: 26px;

    font-weight: bold;

}

#header p {

    font-size: 12px;

}

#header p a {

    text-decoration: none;

}

#header p a:hover {

    border-width: 1px;

}

用 less 咱们就能够这样写:

#header {

    h1 {

        font-size: 26px;

        font-weight: bold;

    }

    p {

        font-size: 12px;

        a {

            text-decoration: none;

            &:hover { border-width: 1px }

        }

    }

}

注意 & 符号的使用—若是你想写串联选择器,而不是写后代选择器,就能够用到 & 了。这点对伪类尤为有用如 :hover。


五、运算

任何数字、颜色或者变量均可以参与运算。

.demo{
    color: #888 / 4;
}

输出:

.demo {
    color: #222222;
}

less 彻底能够进行复杂四则运算,一样的复合运算也没有问题。

六、Color 函数

less 提供了一系列的颜色运算函数。颜色会先被转化成 HSL 色彩空间,而后在通道级别操做。


复制代码
lighten(@color, 10%);     // return a color which is 10% *lighter* than @color
darken(@color, 10%);      // return a color which is 10% *darker* than @color

saturate(@color, 10%);    // return a color 10% *more* saturated than @color
desaturate(@color, 10%);  // return a color 10% *less* saturated than @color

fadein(@color, 10%);      // return a color 10% *less* transparent than @color
fadeout(@color, 10%);     // return a color 10% *more* transparent than @color
fade(@color, 50%);        // return @color with 50% transparency

spin(@color, 10);         // return a color with a 10 degree larger in hue than @color
spin(@color, -10);        // return a color with a 10 degree smaller hue than @color

mix(@color1, @color2);    // return a mix of @color1 and @color2

复制代码

使用起来至关简单:


复制代码
@base: #f04615;

.class {
    color: saturate(@base, 5%);
    background-color: lighten(spin(@base, 8), 25%);
}

复制代码

还能够提取颜色信息:

hue(@color);        // returns the `hue` channel of @color
saturation(@color); // returns the `saturation` channel of @color
lightness(@color);  // returns the 'lightness' channel of @color

例如:

@color: #f36;

#header {
    background-color: hsl(hue(@color),45%,90%);
}

输出:

#header {
    background-color: #f1dae0;
}

七、Math 函数

less 提供了一组方便的数学函数,你可使用它们处理一些数字类型的值。

round(1.67); // returns 2
ceil(2.4);   // returns 3
floor(2.6);  // returns 2

若是你想将一个值转化为百分比,你可使用 percentage 函数:

percentage(0.5); // returns 50%

八、命名空间

有时候,你可能为了更好组织 css 或者单纯是为了更好的封装,将一些变量或者混合模块打包起来,你能够像下面这样在 #form 中定义一些属性集以后能够重复使用:


复制代码
#form {
    .submit () {
        display: block;
        border: 1px solid black;
        background: gray;
        &:hover { background: green }
    }
    .register { ... }
    .login { ... }
}

复制代码

你只须要在 #myform 中像这样引入 .submit:

#myform {
    color: orange;
    #form > .submit;
}

九、做用域

和其余编程语言相似,less 变量也有做用域。首先会从本地查找变量或者混合模块,若是没找到的话会去父级做用域中查找,直到找到为止。


复制代码
@var: red;

#page {
    @var: white;
    #header {
        color: @var; // white
    }
}

#footer {
    color: @var; // red 
}

复制代码

十、注释

css 形式的注释在 less 中是依然保留的,同时 less 也支持双斜线的注释,可是编译成 css 的时候自动过滤掉。

 

最后 less 还有一些其余的特性就不介绍了,你们能够去官网上看下。

相关文章
相关标签/搜索