Sass学习笔记 -- 初步了解函数、运算、条件判断及循环

简介:

函数

sass定义了很多函数可供使用,当然你也可以自己定义函数,以@fuction开始。sass的官方函数链接为:sass fuction,实际项目中我们使用最多的应该是颜色函数,而颜色函数中又以lighten减淡和darken加深为最,其调用方法为lighten($color,$amount)和darken($color,$amount),它们的第一个参数都是颜色值,第二个参数都是百分比。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//scss
$baseFontSize:       10px  ! default ;
$gray:               #ccc  ! default ;        
 
// pixels to rems 
@function pxToRem($px) {
   @return $px / $baseFontSize *  1 rem;
}
 
body{
   font-size :$baseFontSize;
   color :lighten($ gray , 10% );
}
.test{
   font-size :pxToRem( 16px );
   color :darken($ gray , 10% );
}
 
//css
body {
   font-size 10px ;
   color #e6e6e6 ;
}
 
.test {
   font-size 1.6 rem;
   color #b3b3b3 ;
}



运算

sass具有运算的特性,可以对数值型的Value(如:数字、颜色、变量等)进行加减乘除四则运算。请注意运算符前后请留一个空格,不然会出错。

1
2
3
4
5
6
7
8
9
10
11
12
//scss
$baseFontSize:           14px  ! default ;
$baseLineHeight:         1.5  ! default ;
$baseGap:               $baseFontSize * $baseLineHeight ! default ;
$halfBaseGap:           $baseGap /  2   ! default ;
$samllFontSize:         $baseFontSize -  2px   ! default ;
 
//grid 
$_columns:                      12  ! default ;      // Total number of columns
$_column- width :                 60px  ! default ;   // Width of a single column
$_gutter:                       20px  ! default ;     // Width of the gutter
$_gridsystem- width :            $_columns * ($_column-width + $_gutter); //grid system width



条件判断及循环

@if判断

@if可一个条件单独使用,也可以和@else结合多条件使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//scss
$lte 7:  true;
$type: monster;
.ib{
     display :inline- block ;
     @if $lte 7  {
         * display : inline ;
         *zoom: 1 ;
     }
}
p {
   @if $type == ocean {
     color blue ;
   } @else if $type == matador {
     color red ;
   } @else if $type == monster {
     color green ;
   } @else {
     color black ;
   }
}
 
//css
.ib {
   display : inline- block ;
   * display inline ;
   *zoom:  1 ;
}
 
p {
   color green ;
}


三目判断 

语法为:if($condition, $if_true, $if_false) 。三个参数分别表示:条件,条件为真的值,条件为假的值。

1
2
if(true,  1px 2px ) =>  1px
if(false,  1px 2px ) =>  2px


for循环 

for循环有两种形式,分别为:@for $var from <start> through <end>和@for $var from <start> to <end>。

$i表示变量,start表示起始值,end表示结束值,

这两个的区别是关键字through表示包括end这个数,而to则不包括end这个数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//scss
@for $i from  1  through  3  {
   .item-#{$i} {  width 2em  * $i; }
}
 
//css
.item -1  {
   width 2em ;
}
 
.item -2  {
   width 4em ;
}
 
.item -3  {
   width 6em ;
}


@each循环 

语法为:@each $var in <list or map>

其中$var表示变量,而list和map表示list类型数据和map类型数据。sass 3.3.0新加入了多字段循环和map数据循环。 


单个字段list数据循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//scss
$animal-list: puma, sea-slug, egret, salamander;
@each $animal in $animal-list {
   .#{$animal}- icon  {
     background-image url ( '/images/#{$animal}.png' );
   }
}
 
//css
.puma- icon  {
   background-image url ( "/images/puma.png" );
}
 
.sea-slug- icon  {
   background-image url ( "/images/sea-slug.png" );
}
 
.egret- icon  {
   background-image url ( "/images/egret.png" );
}
 
.salamander- icon  {
   background-image url ( "/images/salamander.png" );
}


多个字段list数据循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//scss
$animal-data: (puma,  black default ),(sea-slug,  blue pointer ),(egret,  white move );
@each $animal, $color, $cursor in $animal-data {
   .#{$animal}- icon  {
     background-image url ( '/images/#{$animal}.png' );
     border 2px  solid  $color;
     cursor : $cursor;
   }
}
 
//css
.puma- icon  {
   background-image url ( "/images/puma.png" );
   border 2px  solid  black ;
   cursor default ;
}
 
.sea-slug- icon  {
   background-image url ( "/images/sea-slug.png" );
   border 2px  solid  blue ;
   cursor pointer ;
}
 
.egret- icon  {
   background-image url ( "/images/egret.png" );
   border 2px  solid  white ;
   cursor move ;
}


多个字段map数据循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//scss
$headings: (h 1:  2em , h 2:  1.5em , h 3:  1.2em );
@each $header, $size in $headings {
   #{$header} {
     font-size : $size;
   }
}
 
//css
h 1  {
   font-size 2em ;
}
 
h 2  {
   font-size 1.5em ;
}
 
h 3  {
   font-size 1.2em ;
}


后续详情学习,可参照大漠老师的博客

http://www.w3cplus.com/sassguide/syntax.html


本文转自   frwupeng517   51CTO博客,原文链接:http://blog.51cto.com/dapengtalk/1871533


相关文章
|
6月前
|
Python
python之if语句的单分支,双分支,多分支,if逻辑运算符or,if逻辑运算符and,if语句的嵌套的定义及其使用方法
python之if语句的单分支,双分支,多分支,if逻辑运算符or,if逻辑运算符and,if语句的嵌套的定义及其使用方法
139 0
|
9月前
循环结构--1~100的累加
如果条件为假,不满足时,可能一次都不执行,直接退出循环(这也是与后判断条件形式的区别)
|
6月前
|
Python
python之while循环,无限循环,while循环的嵌套的定义和使用。
python之while循环,无限循环,while循环的嵌套的定义和使用。
|
7月前
js-选择结构与判断分支结构
js-选择结构与判断分支结构
|
8月前
|
C语言 C++
C语言基础--逻辑判断和循环
C语言基础--逻辑判断和循环
44 0
|
9月前
|
小程序
循环结构-用while循环求编写求s=1+(1+2)+(1+2+3)+....+(1+2+3+....+n) 的值
循环结构-用while循环求编写求s=1+(1+2)+(1+2+3)+....+(1+2+3+....+n) 的值
411 0
|
9月前
|
JavaScript C#
js基础②—运算符、流程控制语句、函数
如果变量 age 中的值小于 18,则向变量 voteable 赋值 "年龄太小",否则赋值 "年龄已达到"。
|
9月前
三元表达式竟然可以这样玩(多条件判断无限嵌套)
三元表达式竟然可以这样玩(多条件判断无限嵌套)
45 0
|
XML Java 数据格式
【Lua基础 第3章】变量、赋值语句、索引、lua中的循环、循环控制语句
lua 中的变量、赋值语句、索引、lua中的循环、循环控制语句
118 0
【Lua基础 第3章】变量、赋值语句、索引、lua中的循环、循环控制语句
|
数据采集 大数据 开发者
Lua 语法数值 for 循环与泛型 for 循环|学习笔记
快速学习 Lua 语法数值 for 循环与泛型 for 循环
68 0