sass高級語法


github地址:https://github.com/lily1010/sass/tree/master/course03

用到的sass語法是:

sass --watch test.scss:test.css --style expanded

如下圖:

1 導入外部文件,缺省文件后綴默認是sass/scss文件,一般在頭部聲明

test.scss內容是:

@import "lili.scss";  //導入一個文件
@import "lili.scss", "haha.scss"; //導入兩個文件 
/*但在以下情況下, 僅作為普通的 CSS @import 規則語句,不會導入任何 Sass 文件。
*(1) 如果文件的擴展名是 .css。
*(2) 如果文件名以 http:// 開始。
*(3) 如果文件名是 url()
*(4)如果@import 中包含任何的媒體查詢(media queries)
*/
@import "lili.css";
@import "http://foo.com/bar";
@import url(lili);
@import "lili" screen;

/*在import里面插入動態變量,但是僅適用於url方式*/
$name:family;
@import url("http://fonts.googleapis.com/css?family=#{$name}");

/*導入scss文件,卻不需要將它編譯為css文件做法:
 *(1)新建一個文件夾,為了將不需要編譯的文件和需要編譯的文件分開,這點千萬注意
 *(2)在已經建好的文件夾里面,將不要編譯的*.scss文件命名為_*.scss
 *(3)導入的時候不要用下划線,直接@import("新建文件夾名字/*.scss")
 */

其中lili.scss內容是:

.test1 {
    color: black;
}

其中haha.scss內容是:

.test11 {
    color: deeppink;
}

編譯成test.css內容是:

@import url(lili.css);
@import "http://foo.com/bar";
@import url(lili);
@import "lili" screen;
@import url("http://fonts.googleapis.com/css?family=family");
.test1 {
  color: black;
}

.test1 {
  color: black;
}

.test11 {
  color: deeppink;
}

 

2 extend函數,不只繼承類名選擇器的樣式,還繼承與它相關的樣式,包括繼承它的父選擇器

test.scss內容是:

.test2 {
  border: 1px #f00;
  background-color: #fdd;
}
.test2.test21 {
  background-image: url("/image/hacked.png");
}
.test2 .test22 {
  background-image: url("/image/haha.png");
}
html .test2 {
  width: 100px;
}
.lili {
  @extend .test2;
  border-width: 3px;
} 

編譯成test.css內容是:

.test2, .lili {
  border: 1px #f00;
  background-color: #fdd;
}

.test2.test21, .test21.lili {
  background-image: url("/image/hacked.png");
}

.test2 .test22, .lili .test22 {
  background-image: url("/image/haha.png");
}

html .test2, html .lili {
  width: 100px;
}

.lili {
  border-width: 3px;
}

 

3 extend函數,繼承單元素選擇器樣式,同時繼承與它相關的樣式,包括繼承它的父選擇器

test.scss內容是:

a:hover {
    color: green;
}
a.class1:hover {
    height: 10px;
}
html a:hover {
    width: 10px;
}
.test3 {
    @extend a:hover;
    width: 20px;
}

編譯成test.css內容是:

a:hover, .test3 {
  color: green;
}

a.class1:hover, .class1.test3 {
  height: 10px;
}

html a:hover, html .test3 {
  width: 10px;
}

.test3 {
  width: 20px;
}

 

4 extend中鏈式擴展

test.scss內容是:

.test4 {
    width:20px;
}
.test41 {
    @extend .test4;
    height: 20px;
}
.test42 {
    @extend .test41;
    top:20px;
}

編譯成test.css內容是:

.test4, .test41, .test42 {
  width: 20px;
}

.test41, .test42 {
  height: 20px;
}

.test42 {
  top: 20px;
}

 

5 占位符%,%不會被編譯到css里面

test.scss內容是:

.test5 a%name {
    width: 100px;
}
.lili {
    height: 200%;
    @extend %name;
}

編譯成test.css內容是:

.test5 a.lili {
  width: 100px;
}

.lili {
  height: 200%;
}

 

6 extend中防止繼承不存在的樣式出錯,用!optional直接跳過空樣式

test.scss內容是:

.test6 {
    @extend noexist!optional;
    height: 100px;
}

編譯成test.css內容是:

.test6 {
  height: 100px;
}

 

7 @at-root指令導致一個或多個規則被限定輸出在文檔的根層級上,而不是被嵌套在其父選擇器下

test.scss內容是:

.test7 {
    height: 20px;
    @at-root {
        .children1 {
            color: red;
        }
        .children2 {
            color: black;
        }
    }
}

編譯成test.css內容是:

.test7 {
  height: 20px;
}
.children1 {
  color: red;
}

.children2 {
  color: black;
}

 

8 @at-root(without:類名)將選擇器移動到嵌套指令之外

test.scss內容是:

.gaga {
    @media name {
      .page {
        width: 8px;
        @at-root (without: media) {  //注意此處目前測試是不支持類名的,比如.test8
          color: red;
        }
      }
    }
}

編譯成test.css內容是:

@media name {
  .gaga .page {
    width: 8px;
  }
}
.gaga .page {
  color: red;
}

 

9 if條件判斷,注意不支持if...else...

test.scss內容是:

.test8 {   //if...if..
    @if 1+1 == 2 {
        width: 20px;
    }
    @if 5 < 3 {
        width: 100px;
    }
}
.test81 {  //if...else if...
    @if 1+1 != 2 {
        width: 20px;
    }
    @else if 5 > 3 {
        width: 100px;
    }
}
.test82 {  //if...else if...else...
    @if 1+1 != 2 {
        width: 20px;
    }
    @else if 5 < 3 { 
        width: 100px;
    }
    @else {
        width: 10px;
    }
}

編譯成test.css內容是:

.test8 {
  width: 20px;
}

.test81 {
  width: 100px;
}

.test82 {
  width: 10px;
}

 

10 for循環語句

test.scss內容是:

//第一種格式 @for $var from <start> through <end>,注意范圍包括<start>和<end>的值
@for $i from 1 through 3 {
    .gray#{$i*3} {
        color: #333*$i; 
    }
}

//第二種格式 @for $var from <start> to <end>,注意范圍從<start>開始運行,但不包括<end>的值
@for $i from 1 to 4 {
    .gray2#{$i*3} {
        color: #333*$i; 
    }
}

編譯成test.css內容是:

.gray3 {
  color: #333333;
}

.gray6 {
  color: #666666;
}

.gray9 {
  color: #999999;
}

.gray23 {
  color: #333333;
}

.gray26 {
  color: #666666;
}

.gray29 {
  color: #999999;
}

 

11 each循環語句  @each $var in <list or map>

test.scss內容是:

$name:"lili","yaya","sansa";  //注意數組list的寫法
@each $i in $name {
    test9.#{$i} {
        width: 10px;
    }
}

$name2:(name21:"lili",name22:"yaya",name23:"sansa");  //注意對象map的寫法
@each $i in $name2 {
    test9.#{$i} {
        width: 10px;
    }
}

$name3:(name31:1,name32:2,name33:3);  //注意對象map的寫法
@each $key,$value in $name3 {
    test9.#{$key} {
        width: 10px*$value;
    }
}

編譯成test.css內容是:

test9.lili {
  width: 10px;
}

test9.yaya {
  width: 10px;
}

test9.sansa {
  width: 10px;
}

test9.name21 lili {
  width: 10px;
}

test9.name22 yaya {
  width: 10px;
}

test9.name23 sansa {
  width: 10px;
}

test9.name31 {
  width: 10px;
}

test9.name32 {
  width: 20px;
}

test9.name33 {
  width: 30px;
}

  

12 while循環語句

test.scss內容是:

$i:3;
@while $i > 0 {
    .gray#{$i} {
        color: #333*$i;
    }
    $i:$i - 1; //注意此處不能寫成$i:$i-1,因為會被當成字符串
}

編譯成test.css內容是:

.gray3 {
  color: #999999;
}

.gray2 {
  color: #666666;
}

.gray1 {
  color: #333333;
}

  

13 混入指令,實現代碼塊復用

test.scss內容是:

@mixin left01 {  //不帶參數
    float: left;
}
.test10 {
    @include left01; 
}

@mixin left02($left) {  //帶1個參數
    float: $left;
}
.test101 {
    @include left02(left); 
}

@mixin left03($left,$width) {  //帶2個參數,或者說參數為數組
    float: $left;
    .lili {
        width: $width;
    }
}
.test102 {
    @include left03(left,100px); 
}

@mixin left04($name31,$name32,$name33) {  //參數為對象,但是接受傳遞的參數必須是對象相對應key,同時需要用...傳遞參數
    .lili {
        width: $name31;
        height: $name32;
        top: $name33;
    }
}
$map:(name31:"1px",name32:"2px",name33:"3px");
.test103 {
    @include left04($map...); 
}

@mixin left05($left:right) {  //帶默認參數,不傳參的話就用默認參數
    float: $left;
}
.test104 {
    @include left05; 
}

@mixin box-shadow($shadows...) {  //不定參數,用...
      -moz-box-shadow: $shadows;
      -webkit-box-shadow: $shadows;
      box-shadow: $shadows;
}
.shadows {
  @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}

編譯成test.css內容是:

.test10 {
  float: left;
}

.test101 {
  float: left;
}

.test102 {
  float: left;
}
.test102 .lili {
  width: 100px;
}

.test103 .lili {
  width: "1px";
  height: "2px";
  top: "3px";
}

.test104 {
  float: right;
}

.shadows {
  -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}

 

14 傳遞內容塊@content到混入,傳遞到@content位置

test.scss內容是:

@mixin lala {
    html {
        color: #888;
        @content;
    }
}
@include lala {  //此處名字必須和上面保持一致
    .logo {
        font-size: 15px;
    }
}

編譯成test.css內容是:

html {
  color: #888;
}
html .logo {
  font-size: 15px;
}

 

15 變量在混入@mixin的作用域,即傳遞給混入(mixin)的內容塊在其被定義的作用域中進行運算,而不是混入(mixin)的作用域。這意味着混入(mixin)的局部變量不能傳遞給樣式塊使用

test.scss內容是:

$color: white;
@mixin haha($color:black) {
    background-color: $color;
    @content;
}
.test12 {
    @include haha{
        color: $color;
    }
}

編譯成test.css內容是:

.test12 {
  background-color: black;
  color: white;
}

 

16 函數,用法類似@mixin

test.scss內容是:

@function sasa($name) {
    @return $name;
}
.test13 {
    font-size: sasa(15px);
}

編譯成test.css內容是:

.test13 {
  font-size: 15px;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM