Sass之二(進階篇)
1. 數據類型
1.1 Number
- 數字類型,小數類型,帶有像素單位的數字類型,所有都屬於Number類型
- Number類型詳情請點擊這裏,下面是小例子
$n1: 1.2;
$n2: 12;
$n3: 14px;
$n4: 1em;
1.2 String
- 不加雙引號的,加雙引號的,加單引號的所有都屬於String類型
- String類型詳細內容請點擊這裏, 下面是小demo
$s1: container;
$s2: 'container';
$s3: "container";
1.3 List
- List類型的取值,語法
nth(list, index)
,第一個參數表示要取誰的,也就是list類型的變量的名稱,第二個表示索引,這裏的索引和JavaScript略有不一樣,JavaScript索引基本上都是從零開始,而這裏是從一開始的.
- List類型的方法的詳情請點擊這裏, 下面是小demo
$padding: 1px 5px 10px 15px;
.container {
padding: nth($padding,2) nth($padding,4);
}
.container { padding: 5px 15px; }
1.4 Map
- Map類型取值,語法
map-get(map, key)
,第一個參數表示要取誰的,也就是map類型的變量的名稱,第二個參數表示要取的key
- Map類型的方法的詳情請點擊這裏, 下面是小demo
$map: (color: red,background-color: #f00);
body {
color: map-get($map, color);
}
body { color: red; }
1.5 Color
$c1: blue;
$c2: #fff;
$c3: rgba(255,255,0,0.5);
body {
color: $c3;
}
body { color: rgba(255, 255, 0, 0.5); }
1.6 Boolean
- 布爾類型分爲兩種
true
和false
$bt: true;
$bf: false;
$null: null;
body {
@if $null == null{
color: red;
}
}
body { color: red; }
2. 變量的操做
2.3 +,-,*,/,%
- 進行計算的時候最好用空格進行隔開,例如減號若是不隔開,會把兩個變量當成一個變量來處理.
- 下面是一些小例子
$width1: 100px;
$width2: 125px;
span {
width: $width1 + $width2;
}
a:hover {
cursor: e + -resize;
}
a::before {
content: A + 'bc';
}
a::before {
content: "A" + bc;
}
p {
padding: 3px + 4px auto;
}
$version: 3;
p::before {
content: "hello,Sass #{$version}";
}
p {
font: (20px/10px);
width: $width2 / 2;
width: round($width2 / 2);
height: (100px / 2);
}
span { width: 225px; }
a:hover { cursor: e-resize; }
a::before { content: Abc; }
a::before { content: "Abc"; }
p { padding: 7px auto; }
p::before { content: "hello,Sass 3"; }
p { font: 2; width: 62.5px; width: 63px; height: 50px; }
3.Mixin
3.1簡單實例
- 學過JavaScript的都對方法耳熟能詳,那麼Scss中的mixin就相似於JavaScript中的方法
@mixin test1 {
color: red;
}
body {
@include test1;
}
@mixin test2($color: red) {
color: $color;
}
body {
@include test2(#fff);
}
@mixin test3($color: red, $fontSize: 12px) {
color: $color;
font-size: $fontSize;
}
body {
@include test3($fontSize: 18px, $color: blue);
}
body { color: red; }
body { color: #fff; }
body { color: blue; font-size: 18px; }
3.2 傳遞多值參數
- 傳遞多值參數的時候,須要在參數後面加上三個點
...
表示這個參數可能含有多條屬性,告訴sass不要區分逗號了,把傳遞的參數當作一個參數來解析.
@mixin test4($shadow...) {
text-shadow: $shadow;
-webkit-text-shadow: $shadow;
-moz-text-shadow: $shadow;
}
.text {
@include test4(1px 1px 10px red, 0 0 5px blue);
}
.text { text-shadow: 1px 1px 10px red, 0 0 5px blue; -webkit-text-shadow: 1px 1px 10px red, 0 0 5px blue; -moz-text-shadow: 1px 1px 10px red, 0 0 5px blue; }
3.3 傳遞內容
- 傳遞內容就比如在方法中弄了個佔位符, 當你調用的時候,所寫的內容會直接放在佔位符那裏;
@mixin style-for-iphone {
@media only screen and (min-width:320px) and (max-width:568px){
@content;
}
}
@include style-for-iphone {
height: 100px;
font-size: 12px;
}
@media only screen and (min-width: 320px) and (max-width: 568px) { height: 100px; font-size: 12px; }
4. 函數
4.1 內置函數
- 內置函數是系統給咱們定義好了的一些函數,詳情請點擊這裏
4.2 自定義函數
- 提到函數咱們不有自主的想到JavaScript中的函數,Sass須要在
function
和return
前面加一個@
符號,例如:
@function double($width){
@return $width * 2;
}
body {
$color: rgba(255,0,0, .3);
color: $color;
width: 100%;
height: 100%;
p {
color: darken($color, 0.5);
background-color: lighten($color, 0.9);
z-index: str-length('aaaa');
z-index: str-index('aaabddd','b');
width: double(5px);
}
}
body { color: rgba(255, 0, 0, 0.3); width: 100%; height: 100%; }
body p { color: rgba(252, 0, 0, 0.3); background-color: rgba(255, 5, 5, 0.3); z-index: 4; z-index: 4; width: 10px; }
5. 編譯輸出
5.3 error
6. 條件語句及循環語句
$width: 800;
body {
color: if($width > 800, blue, red);
}
@if $width > 800 {
body {
color: red;
}
}
@else if $width == 800 {
p {
color: blue;
}
}
@else {
body{
color: blue;
}
}
body { color: red; }
p { color: blue; }
6.2 循環語句
- 語法1
for $i from 1 to 10
- 語法2
for $i from 1 through 10
- 下面代碼是
through
的用法,to的用法在這裏就不演示了
@for $i from 1 through 5 {
.span#{$i} {
width: 20% * $i;
}
}
.span1 { width: 20%; }
.span2 { width: 40%; }
.span3 { width: 60%; }
.span4 { width: 80%; }
.span5 { width: 100%; }
6.3 while
- 使用while 須要注意一下幾點,
- 第一: 變量要提早聲明
- 第二: while裏面能夠設置步長
$j: 6;
@while $j > 0 {
.p#{$j} {
width: 20% * $j;
}
$j: $j - 3;
}
.p6 { width: 120%; }
.p3 { width: 60%; }
7. each
7.1 常規遍歷
$k: 1;
$color: red, green, blue;
@each $c in $color {
.div#{$k} {
color: $c;
}
$k: $k + 1;
}
.div1 { color: red; }
.div2 { color: green; }
.div3 { color: blue; }
7.2 遍歷list
- 這裏出現的方式是以一個鍵一個值的形式出現的,若是是但數據的變量,那麼能夠在外邊頂一個索引,內部執行加1 操做,也能夠獲得相應的效果.
$key
表示鍵值,$color
表示值
@each $key, $color in (default, green), (dange,blue), (error, red) {
.aler-#{$key} {
color: $color;
}
}
.aler-default { color: green; }
.aler-dange { color: blue; }
.aler-error { color: red; }
@each $key, $val in (default: green, dange: blue, error: red) {
.alert-#{key} {
color: $val;
}
}
.alert-key { color: green; }
.alert-key { color: blue; }
.alert-key { color: red; }
8. 小實例
- 這裏寫了個小實例, 有興趣的朋友能夠看看,其實我也是抄的別人的實例,等會去寫個小項目,orz我要睡覺了,明天寫吧….好累的說,代碼粘在下面了…
@function buildContainer($num: 5) {
$map: (defaultValue: 0);
$rate: percentage(1 / $num);
@for $i from 1 through 5 {
$tempMap: (col#{$i} : $rate * $i);
$map: map-merge($map, $tempMap);
}
$map: map-remove($map, defaultValue);
@return $map;
}
@mixin buildContainer($num: 5) {
$map: buildContainer($num);
@each $key, $val in $map {
.#{$key} {
width: $val;
}
}
}
@include buildContainer();
.col1 { width: 20%; }
.col2 { width: 40%; }
.col3 { width: 60%; }
.col4 { width: 80%; }
.col5 { width: 100%; }