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 }
◑