在 Sass 中除了可以定义变量,具有 @extend 、 %placeholder 和 mixins 等特性之外,还自备了一系列的函数功能。其主要包括:
● 字符串函数
● 数字函数
● 列表函数
● 颜色函数
● Introspection 函数
● 三元函数等
当然除了自备的函数功能之外,我们还可以根据自己的需求定义函数功能,常常称之为 自定义函数 。
字符串函数
字符串函数顾名思意是用来处理字符串的函数。Sass 的字符串函数主要包括 两个函数 :
unquote($string): 删除字符串中的引号;quote($string):给字符串添加引号。
1、unquote()函数
unquote() 函数主要是用来 删除 一个 字符串中的引号 ,如果这个字符串没有 带有引号 ,将 返回原始的字符串 。
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 代码:
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() 函数只能删除字符串最 前 和 最后 的 引号 (双引号或单引号),而无法删除字符串中间的引号。如果字符没有带引号,返回的将是字符串本身。
2、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()
1、To-upper-case()
To-upper-case() 函数 将字符串小写字母转换成大写字母 。如:
1 //SCSS2 .test {3 text: to-upper-case(aaaaa);4 text: to-upper-case(aA-aAAA-aaa);5 }
编译出来的 css 代码:
1 //CSS2 .test {3 text: AAAAA;4 text: AA-AAAA-AAA;5 }
2、To-lower-case()
To-lower-case() 函数与 To-upper-case() 刚好 相反 ,将字符串 转换成小写字母 :
1 //SCSS2 .test {3 text: to-lower-case(AAAAA);4 text: to-lower-case(aA-aAAA-aaa);5 }
编译出来的 css 代码:
1 //CSS2 .test {3 text: aaaaa;4 text: aa-aaaa-aaa;5 }
◑
查看更多关于Sass函数字符串函数_html/css_WEB-ITnose的详细内容...