Less與Sass是css的預處理技術

而CoffeeScript、TypeScript則是javascript的預處理技術。

一、Less

Less是一種動態樣式語言,是一門css預處理語言,擴充套件了css語言,增加了變數,運算,巢狀等一系列功能

1.1 變數

語法:@變數名:變數值;

存在作用域:區域性作用域>全域性作用域

以@開頭,後面輸入變數名.

例項:

  1. @color:red;
  2. @size:18px;
  3. .div{
  4. background-color:@color;
  5. font-size:@size;
  6. ...
  7. }
  8. //翻譯後
  9. .div{
  10. background-color:red;
  11. font-size:@size;
  12. ...
  13. }

1.2 混入

定義一個函式,寫入引數值,可呼叫

內建變數@arguments代表所有引數(執行時)的值

相當於一個模板,將模板引入,再寫入引數即可使用

如:

  1. .circle(@width:100px,@color:red,@height:100px){
  2. width:@width;
  3. color:red;
  4. height:100px;
  5. font-size:18px;
  6. }
  7. .c1{
  8. .circle(); //不帶引數引入
  9. .circle(200px,blue)//帶兩個引數
  10. .circle
  11. ...
  12. }
  13. //翻譯後
  14. .c1{
  15. width:100px;
  16. //帶引數為: width:200px;
  17. color:red;
  18. //帶引數為: color:blue;
  19. height:100px;
  20. font-size:18px;
  21. }

1.3 巢狀

允許將多個選擇器巢狀在一起,

&表示當前選擇器的父選擇器

例:

  1. .cor{
  2. color:red;
  3. .ten{font-size:12px;}
  4. &#sub{width:100px;}
  5. }
  6. //翻譯後
  7. .cor{
  8. color:red;
  9. }
  10. .cor .ten{
  11. font-size:12px;
  12. }
  13. .cor#sub{
  14. width:100px;
  15. }

1.4 運算

less擁有強大的運算能力,針對數字,顏色,變數的操作,支援加、減、乘、除或更復雜的綜合運算!

例:

  1. @file:5%;
  2. @base-color:lightblue;
  3. .cor{
  4. width:@file * 10; //50%
  5. background-color: @base-color + #111;
  6. }

1.5 繼承

讓一個樣式繼承另一個樣式

例:

  1. .cor{
  2. width:100px;
  3. color:red;
  4. }
  5. .cor2{
  6. &:extend(.cor);
  7. font-size:15px;
  8. }
  9. .cor3{
  10. &:extend(.cor);
  11. }
  12. //翻譯後
  13. .cor,
  14. .cor2,
  15. .cor3{
  16. width:100px;
  17. color:red;
  18. }
  19. .cor2{
  20. font-size:15px;
  21. }

1.6 作用域

區域性作用域>全域性作用域

同一級變數,後者會覆蓋前者

例:

  1. @name:30px;
  2. .cor{
  3. @name:20px;
  4. font-size:@name; //20px
  5. }
  6. @name:18px
  7. .cor2{
  8. font-size:@name; //18px
  9. }

1.7 迴圈

在Less中,可以呼叫本身

例:

  1. .cor(@name) when (@name > 0){
  2. .cor((@name - 1))
  3. width:(@name * 10px);
  4. }
  5. .ten{
  6. .cor(3);
  7. }
  8. //翻譯後
  9. .ten{
  10. width:10px;
  11. width:20px;
  12. width:30px;
  13. }

二、Sass

2.1 變數

使用符號:$識別符號

  1. <!--定義變數-->
  2. $width:1000px;
  3. $height:500px;
  4. .box{
  5. width:$width;
  6. height:$height;
  7. }

2.2 巢狀

略,與less相同

2.3 匯入

@import

reset.scss 檔案

  1. $width:900px;
  2. $color:blue;
  3. *{
  4. background-color:$color;
  5. width:$width;
  6. }

scss程式碼

  1. @import "reset"
  2. .box{
  3. width:200px;
  4. }
  5. <!--翻譯後-->
  6. *{
  7. background-color:$color;
  8. width:$width;
  9. }
  10. .box{
  11. width:200px;
  12. }

2.4 mixin 混入

sass中用mixin定義一些程式碼段,可傳引數,

定義時用@mixin

呼叫時用@include

例:

  1. @mixin cir($size:100px,$color:red,$px:14px){
  2. width:$size;
  3. background-color:$color;
  4. font-size:$px;
  5. }
  6. .cls{
  7. @include cir();<!--呼叫-->
  8. @include cir(150px);
  9. @include cir(150px,blue,20px);
  10. }
  11. 翻譯後
  12. .cls{
  13. width:100px;
  14. background-color:red;
  15. font-size:14px;
  16. ------------------------
  17. width:150px;
  18. background-color:red;
  19. font-size:14px;
  20. ------------------------
  21. width:150px;
  22. background-color:blue;
  23. font-size:20px;
  24. }

2.5 繼承/擴充套件

通過@extend來實現程式碼組合

例:

  1. .state{
  2. background-color:red;
  3. }
  4. .state2{
  5. @extend .state
  6. background-color:blue;
  7. font-size:12px;
  8. }
  9. 翻譯後
  10. .state,.state2{
  11. background-color:red;
  12. }
  13. .state2{
  14. background-color:blue;
  15. font-size:12px;
  16. }

2.6 sass運算

sass可以進行復雜的運算

2.7 函式

sass集成了大量的顏色函式,讓變換顏色更加簡單

例:

  1. $pcolor:#999ccc;
  2. .cls:hover{
  3. darken:黑暗
  4. background-color:darken($pcolor,15%);
  5. <!--變暗15%-->
  6. lighten:照亮
  7. background-color:lighten($pcolor,15%);
  8. <!--變亮15%-->
  9. }

2.8 流程控制

sass中擁有流程控制語句,如:if,for,while,each等

例:

  1. /*如果i是3的倍數,則下劃線*/
  2. @if $i%3==0 {
  3. text-decoration: underline;
  4. }