
玩玩Sass 自帶的一些函數,有字元串函數(String Functions)、數字函數(Number Functions)、清單函數(List Functions)、顔色函數(Color Functions)、Introspection 函數(Introspection Functions)、三元函數(Miscellaneous Function)
1. 字元串函數
1.1 quote()
quote($string)給$string前後添加引号。
//Scss:
p:after{
content: quote(hello +' '+ sass);
//中間有空格(其他特殊符号)需要拼字元串;quote(hello sass); 直接這樣會報錯;
}
p:before{
content: quote('This\'s' + ' ' + 'bug');
//如果$string本身就帶有引号,則會統一換成雙引号`""`;
}
//編譯為:
p:after { content: "hello sass"; }
p:before { content: "This's bug"; }
1.2 unquote()
unquote($string) 删除 $string 前後的引号。
//Scss:
p:after{
content: unquote("This's bug"); //中間的引号未被删除;
}
p:before{
content: unquote(Thissbug);
//如$string本身就不帶引号,則傳回$string本身;
}
//編譯為:
p:after { content: This's bug; }
p:before { content: sass; }
其實吧!這倆玩意在項目中我還真沒用到過!
1.3 str-length()
str-length($string) 傳回 $string 的長度。
//SCSS:
p:before { content: str-length('hello sass!'); }
//編譯為:
p:before { content: 11; }
1.4 to-upper-case()
to-upper-case($string) 将$string小寫字母轉成大寫字母。
//SCSS:
p:before { content: to-upper-case('hello sass!'); }
//編譯為:
p:before { content: "HELLO SASS!"; }
1.5 to-lower-case()
to-lower-case($string) 将$string大寫字母轉成小寫字母。
//SCSS:
p:before { content: to-lower-case('HELLO SASS!'); }
//編譯為:
p:before { content: "hello sass!"; }
2、數字函數
2.1 percentage()
percentage($number) 将一個不帶機關的數值轉成百分比。
//SCSS:
.box{ width: percentage(.5)}
.box2{ width: percentage(.1rem / .3rem)}
經測試,兩個相同的機關,除了用除法 '/' 其他+-%均會報錯,且用除法 '/' 也隻能在兩個相同類型的機關之間進行運算;*
//編譯為:
.box { width: 50%; }
.box2 { width: 33.33333%; }
2.2 round()
round($number) 将$number 四舍五入為整數,$number可帶機關。
//SCSS:
.xs-row{ width: round(33.33333333333333px)}
//編譯為:
.xs-row { width: 33px; }
2.3 ceil()
ceil($number) 大于 $number ,向上取整。
//SCSS
.fs14{ font-size: ceil(13.1px)}
.fs16{ font-size: ceil(15.9px)}
//編譯為:
.fs14 { font-size: 14px; }
.fs16 { font-size: 16px; }
2.4 floor()
與 ceil()相反,floor($number) 去除 $number 小數,向下取整。
//SCSS:
.fs14{ font-size: floor(14.1px) }
.fs16{ font-size: floor(16.9px) }
//編譯為:
.fs14 { font-size: 14px; }
.fs16 { font-size: 16px; }
2.5 abs()
abs($number),傳回 $number 的絕對值。
//SCSS:
.fs16{ font-size: abs(-1.6rem) }
.fs18{ font-size: abs(1.8rem) }
//編譯為:
.fs16{ font-size: 1.6rem; }
.fs18{ font-size: 1.8rem; }
2.6 min() max()
min($numbers…),傳回 $number... 的最小值。
max($numbers…),傳回 $number... 的最大值。
//SCSS:
div{ width: min(2rem, 10rem) }
div{ width: max(2rem, 10rem) }
div{ width: max(2px, 10rem) } //非相同的機關,報錯
//編譯為:
div { width: 2rem; }
div { width: 10rem; }
Incompatible units: 'rem' and 'px'
2.7 random()
random([$limit]),傳回一個随機數。
//SCSS:
div {
height: random(); //直接調用
width: random(666); //傳個參
}
//編譯為:
div {
height: 0.3649;
width: 403;
}
3、清單函數
常用
3.1 length() nth()
length($list),傳回 $list 的長度值;
nth($list, $n),傳回 $list 中指定的某個 $n,且 $n必須是大于0的整數;
JavaScript的Array()的索引是從0開始的,厄...有點扯遠了,用過 css3 的 :nth-child(n)都應該知道啦,索引下标也是從1開始的,So.....
//SCSS:
$list: google, baidu, sogo;
@for $i from 1 through length($list){ //取$list的length并循環輸出;
.icon-#{nth($list, $i)}{ //$list中的某個索引值;
content: '#{nth($list, $i)}'
}
}
//編譯為:
.icon-google { content: "google"; }
.icon-baidu { content: "baidu"; }
.icon-sogo { content: "sogo"; }
3.2 join()
join($list1, $list2, [$separator]) 将兩個清單給連接配接在起來,組成一個清單;
$separator 預設值是 auto,另外還有 comma 和 space 兩個值,其中 comma 值指定清單中的清單項值之間使用逗号 , 隔開,space 值指定清單中的清單項值之間使用 空 格 分隔。
join((blue, red), (#abc, #def), space) => blue red #abc #def //space
join(10px, 20px, comma) => 10px, 20px //comma
Examples:
//SCSS:
$list1: google, baidu, sogo;
$list2: facebook, instagram, twitter;
$list3: join($list1, $list2); //講真啦,很少用到join(),反正我是很少用到;
@for $i from 1 through length($list3){
.icon-#{nth($list3, $i)}{
content: '#{nth($list3, $i)}'
}
}
//編譯為:
.icon-google { content: "google"; }
.icon-baidu { content: "baidu"; }
.icon-sogo { content: "sogo"; }
.icon-facebook { content: "facebook"; }
.icon-instagram { content: "instagram"; }
.icon-twitter { content: "twitter"; }
3.3 index()
index($list, $value),傳回 $list 中的 $value所在的位置。
index(1px solid red, solid) => 2
index(1px solid red, dashed) => null
index((width: 10px, height: 20px), (height 20px)) => 2
3.4 list-separator()
list-separator($list),傳回 $list 中的分隔符。
list-separator(1px 2px 3px) => space
list-separator(1px, 2px, 3px) => comma
list-separator('foo') => space
4、Introspection 函數
4.1 type-of()
type_of($value) 傳回 $value 的類型。
type-of(abc) => string
type-of("abc") => string
type-of(true) => bool
type-of(#fff) => color
type-of(green) => color
4.2 unit()
unit($number) 傳回 $number 所使用的機關。
unit(100) => ""
unit(100px) => "px"
unit(3em) => "em"
unit(10px * 5em) => "em*px"
unit(10px * 5em / 30cm / 1rem) => "em*px/cm*rem"
4.3 unitless()
unitless($number) 傳回 $number 是否帶有機關;如果不帶機關傳回值為 true,帶機關傳回值為 false。
unitless(100) => true
unitless(100px) => false
5、 Miscellaneous 函數
Miscellaneous 函數稱為三元條件函數,主要因為他和 JavaScript 中的三元判斷非常的相似。他有兩個值,當條件成立傳回一種值,當條件不成立時傳回另一種值
if($condition, $if-true, $if-false)
當 $condition 條件為真,則傳回 $if-true 值,否則傳回 $if-false值。
if(true, 1px, 2px) => 1px
if(false, 1px, 2px) => 2px
6、 顔色函數
暫時還沒用到過。
6.1 RGB函數()
rgb($red, $green, $blue),根據$red、$green、$blue三個值建立一個顔色;
rgba($red, $green, $blue, $alpha),将一個顔色根據透明度轉換成 rgba 顔色。
rgba(#102030, 0.5) => rgba(16, 32, 48, 0.5)
rgba(blue, 0.2) => rgba(0, 0, 255, 0.2)
7、Reference API
Sass::Script::Functions — Sass Documentation
結語
既然你都看到這裡了,我就說說吧,這些個函數其實也就在自己寫插件的時候有用,其他時候可能會有些大材小用。
學習更多技能
請點選下方公衆号