Sass的函數簡介
在 Sass 中除了能夠定義變量,具備 @extend、%placeholder 和 mixins 等特性以外,還自備了一系列的函數功能。其主要包括:
● 字符串函數css
● 數字函數函數
● 列表函數spa
● 顏色函數code
● Introspection 函數blog
● 三元函數等字符串
固然除了自備的函數功能以外,咱們還能夠根據本身的需求定義函數功能,經常稱之爲自定義函數。編譯器
字符串函數
字符串函數顧名思意是用來處理字符串的函數。Sass 的字符串函數主要包括兩個函數:scss
unquote($string): 刪除字符串中的引號;
quote($string):給字符串添加引號。
一、unquote()函數
unquote() 函數主要是用來刪除一個字符串中的引號,若是這個字符串沒有帶有引號,將返回原始的字符串。string
1 //SCSS 2 .test1 {
3 content: unquote('Hello Sass!') ;
4 }
5 .test2 {
6 content: unquote("'Hello Sass!");
7 }
8 .test3 {
9 content: unquote("I'm Web Designer");
10 }
11 .test4 {
12 content: unquote("'Hello Sass!'");
13 }
14 .test5 {
15 content: unquote('"Hello Sass!"');
16 }
17 .test6 {
18 content: unquote(Hello Sass);
19 }
編譯後的 css 代碼:io
1 //CSS 2 .test1 {
3 content: Hello Sass!;
4 }
5 .test2 {
6 content: 'Hello Sass!;
7 }
8 .test3 {
9 content: I'm Web Designer;
10 }
11 .test4 {
12 content: 'Hello Sass!';
13 }
14 .test5 {
15 content: "Hello Sass!";
16 }
17 .test6 {
18 content: Hello Sass;
19 }
注意: unquote() 函數只能刪除字符串最前和最後的引號(雙引號或單引號),而沒法刪除字符串中間的引號。若是字符沒有帶引號,返回的將是字符串自己。
二、quote()函數
quote() 函數恰好與 unquote() 函數功能相反,主要用來給字符串添加引號。若是字符串,自身帶有引號會統一換成雙引號 ""。
如:
1 //SCSS 2 .test1 {
3 content: quote('Hello Sass!');
4 }
5 .test2 {
6 content: quote("Hello Sass!");
7 }
8 .test3 {
9 content: quote(ImWebDesigner);
10 }
11 .test4 {
12 content: quote(' ');
13 }
編譯出來的 css 代碼:
1 //CSS 2 .test1 {
3 content: "Hello Sass!";
4 }
5 .test2 {
6 content: "Hello Sass!";
7 }
8 .test3 {
9 content: "ImWebDesigner";
10 }
11 .test4 {
12 content: "";
13 }
使用 quote() 函數只能給字符串增長雙引號,並且字符串中間有單引號或者空格時,須要用單引號或雙引號括起,不然編譯的時候將會報錯。
1 .test1 {
2 content: quote(Hello Sass);
3 }
4 //這樣使用,編譯器立刻會報錯:error style.scss (Line 13: $string: ("Hello""Sass") is not a string for `quote')
解決方案就是去掉空格,或者加上引號:
1 .test1 {
2 content: quote(HelloSass);
3 }
4 .test1 {
5 content: quote("Hello Sass");
6 }
同時 quote() 碰到特殊符號,好比: !、?、> 等,除中折號 - 和 下劃線_ 都須要使用雙引號括起,不然編譯器在進行編譯的時候一樣會報錯。
字符串函數-To-upper-case()、To-lower-case()
一、To-upper-case()
To-upper-case() 函數將字符串小寫字母轉換成大寫字母。如:
1 //SCSS 2 .test {
3 text: to-upper-case(aaaaa);
4 text: to-upper-case(aA-aAAA-aaa);
5 }
編譯出來的 css 代碼:
1 //CSS 2 .test {
3 text: AAAAA;
4 text: AA-AAAA-AAA;
5 }
二、To-lower-case()
To-lower-case() 函數與 To-upper-case() 恰好相反,將字符串轉換成小寫字母:
1 //SCSS 2 .test {
3 text: to-lower-case(AAAAA);
4 text: to-lower-case(aA-aAAA-aaa);
5 }
編譯出來的 css 代碼:
1 //CSS 2 .test {
3 text: aaaaa;
4 text: aa-aaaa-aaa;
5 }
◑