less——動態的css

 

  1. .border-radius (@radius: 5px) {  
  2.     border-radius: @radius;  
  3.     -moz-border-radius: @radius;  
  4.     -webkit-border-radius: @radius;  
  5. }  
  6.  
  7. #header {  
  8.     .border-radius;    

 

 

 

Less 是一種樣式語言,它將 css 賦予了動態語言的特性,如變量、 繼承、 運算、 函數。less 既可以在客戶端上運行(支持IE 6+, Webkit, Firefox),也可以藉助 Node.js 或者 Rhino 在服務端運行。

Less 做爲 css 的一種形式的擴展,它並沒有閹割 css 的功能,而是在現有的 css 語法上,添加了很多額外的功能,所以對於前端開發人員來所,學習 less 是一件輕而易舉的事情。我們先看下用 less 寫的一段 css:

 

  1. @base: #f938ab;  
  2.  
  3. .box-shadow(@style, @c) when (iscolor(@c)) {  
  4.     box-shadow:         @style @c;  
  5.     -webkit-box-shadow: @style @c;  
  6.     -moz-box-shadow:    @style @c;  
  7. }  
  8. .box-shadow(@style, @alpha: 50%) when (isnumber(@alpha)) {  
  9.     .box-shadow(@style, rgba(000, @alpha));  
  10. }  
  11. .box {   
  12.     color: saturate(@base, 5%);  
  13.     border-color: lighten(@base, 30%);  
  14.     div { .box-shadow(0 0 5px30%) }  

在沒有學過 less 的情況下,我們並不知道這些代碼是做啥用的,怎麼生成我們所熟悉的 css 代碼,以上代碼經過 less 編譯後:

 

  1. .box {  
  2.     color#fe33ac;  
  3.     border-color#fdcdea;  
  4. }  
  5. .box div {  
  6.     box-shadow: 0 0 5px rgba(0000.3);  
  7.     -webkit-box-shadow: 0 0 5px rgba(0000.3);  
  8.     -moz-box-shadow: 0 0 5px rgba(0000.3);  

 

下面我們就一起來學習 less 吧。

我們知道如果要使用 jquery 就必須在頁面上引進 jquery 庫,同樣的在使用 less 編寫 css 代碼時,也要引進 less 庫——less.js。點擊這裏下載 less 庫。

下載好後只要在頁面中引入就可以了

 <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 前先引入。

引入了 less 之後,正式開始學習 less。

LESS 語法

1、變量

Less 的變量充許你在樣式中對常用的屬性值進行定義,然後應用到樣式中,這樣只要改變變量的值就可以改變全局的效果。和 javascript 中的全局變量有點類似。

甚至可以用變量名定義爲變量。

 

  1. @colorred;  
  2. @foot: 'color';  
  3.  
  4. .head{  
  5.     color: @color;  
  6. }  
  7.  
  8. .foot{  
  9.     color: @@foot;  

 

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

注意 less 中的變量爲完全的“常量”,所以只能定義一次。

2、混合

混合就是定義一個 class,然後在其他 class 中調用這個 class。

 

  1. .common{  
  2.     colorred;  
  3. }  
  4.  
  5. .nav{  
  6.     background#ccc;  
  7.     .common;  

輸出:

  1. .common {  
  2.   colorred;  
  3. }  
  4. .nav {  
  5.   background#ccc;  
  6.   colorred;  

Css 中的 class, id 或者元素屬性集都可以用同樣的方式引入。

 

3、帶參數混合

在 less 中,你可以把 class 當做是函數,而函數是可以帶參數的。

 

  1. .border-radius (@radius) {  
  2.     border-radius: @radius;  
  3.     -moz-border-radius: @radius;  
  4.     -webkit-border-radius: @radius;  
  5. }  
  6.  
  7. #header {  

 

  1.  
  2.     .border-radius(4px);  
  3. }  
  4. .button {  
  5.     .border-radius(6px);    

 

最後輸出:
 

  1. #header {  
  2.     border-radius: 4px;  
  3.     -moz-border-radius: 4px;  
  4.     -webkit-border-radius: 4px;  
  5. }  
  6. .button {  
  7.     border-radius: 6px;  
  8.     -moz-border-radius: 6px;  
  9.     -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中去,但是還想在其他的屬性集合中引用,就會發現這個方法非常的好用:
 

  1. .wrap () {  
  2.     text-wrap: wrap;  
  3.     white-space: pre-wrap;  
  4.     white-space: -moz-pre-wrap;  
  5.     word-wrap: break-word;  
  6. }  
  7.  
  8. pre {  
  9.     .wrap   

輸出:

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


 

混合還有個重要的變量@arguments。

@arguments 包含了所有傳遞進來的參數,當你不想處理個別的參數時,這個將很有用。
 

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

輸出:

 
.demo {
    border: 2px solid #ff0000;
}

混合還有許多高級的應用,如模式匹配等。在這裏就不介紹了,只講些基礎的東西。
 

4、嵌套規則

Less 可以讓我們用嵌套的方式來寫 css。下面是我們平時寫的 css:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章