目錄
1. 数据类型
2. 变量的操作
3.Mixin
4. 函数
5. 编译输出
6. 条件语句及循环语句
7. each
8. 小实例
首頁 web前端 html教學 Sass进阶之路,之二(进阶篇)_html/css_WEB-ITnose

Sass进阶之路,之二(进阶篇)_html/css_WEB-ITnose

Jun 24, 2016 am 11:26 AM

Sass之二(进阶篇)

1. 数据类型

1.1 Number

  • 数字类型,小数类型,带有像素单位的数字类型,全部都属于Number类型
  • Number类型详情请点击 这里,下面是小例子
1.$n1: 1.2;2.$n2: 12;3.$n3: 14px;4.$n4: 1em;
登入後複製

1.2 String

  • 不加双引号的,加双引号的,加单引号的全部都属于String类型
  • String类型详细内容请点击 这里, 下面是小demo

1.$s1: container;2.$s2: 'container';3.$s3: "container";
登入後複製

1.3 List

  • List类型的取值,语法 nth(list, index),第一个参数表示要取谁的,也就是list类型的变量的名称,第二个表示索引,这里的索引和JavaScript略有不同,JavaScript索引基本上都是从零开始,而这里是从一开始的.
  • List类型的方法的详情请点击 这里, 下面是小demo

1.$padding: 1px 5px 10px 15px;2..container {3.    padding: nth($padding,2) nth($padding,4);4.}5.6.// out css7..container { padding: 5px 15px; }
登入後複製

1.4 Map

  • Map类型取值,语法 map-get(map, key),第一个参数表示要取谁的,也就是map类型的变量的名称,第二个参数表示要取的 key
  • Map类型的方法的详情请点击 这里, 下面是小demo

1.$map: (color: red,background-color: #f00);2.3.body {4.    color: map-get($map, color);5.}6.7.// out css8.body { color: red; }
登入後複製

1.5 Color

1./*! 颜色类型*/2.$c1: blue;3.$c2: #fff;4.$c3: rgba(255,255,0,0.5);5.body {6.    color: $c3;7.}8.9.// out css10.body { color: rgba(255, 255, 0, 0.5); }
登入後複製

1.6 Boolean

  • 布尔类型分为两种 true和 false
  • $bt: true;
  • $bf: false;

1.7 Null

  • null的应用场景如下

1.$null: null;2.body {3.    @if $null == null{4.        color: red;5.    }6.}7.8.// out css9.body { color: red; }
登入後複製

2. 变量的操作

2.1 ==, !=

  • 支持所有的类型

2.2 <,>,<=,>=

  • 只支持Number类型

2.3 +,-,*,/,%

  • 进行计算的时候最好用空格进行隔开,例如减号如果不隔开,会把两个变量当成一个变量来处理.
  • 下面是一些小例子

1.// scss 2.$width1: 100px;3.$width2: 125px;4.span {5.    width: $width1 + $width2;6.}7.8.a:hover {9.    cursor: e + -resize;10.}11.a::before {12.    content: A + 'bc';13.}14.a::before {15.    content: "A" + bc;16.}17.p {18.    padding: 3px + 4px auto;19.}20.$version: 3;21.p::before {22.    content: "hello,Sass #{$version}"; /*! 这里如果加上大括号就会输出3,不加的话,会直接输出$version */23.}24.25.p {26.    font: (20px/10px);27.    width: $width2 / 2;28.    width: round($width2 / 2);29.    height: (100px / 2); /*! 这里如果想让100px/2 起作用的话需要加上一个小括号,告诉Sass这是一个表达式,让它进行计算*/30.}31.32.// out css33.span { width: 225px; }34.a:hover { cursor: e-resize; }35.a::before { content: Abc; }36.a::before { content: "Abc"; }37.p { padding: 7px auto; }38.p::before { content: "hello,Sass 3"; /*! 这里如果加上大括号就会输出3,不加的话,会直接输出$version */ }39.p { font: 2; width: 62.5px; width: 63px; height: 50px; /*! 这里如果想让100px/2 起作用的话需要加上一个小括号,告诉Sass这是一个表达式,让它进行计算*/ }40.41.
登入後複製

3.Mixin

3.1简单实例

  • 学过JavaScript的都对方法耳熟能详,那么Scss中的mixin就类似于JavaScript中的方法

1.// 没有参数的mixin2.@mixin test1 {3.    color: red;4.}5.6.body {7.    @include test1;8.}9.10.// 一个参数11.@mixin test2($color: red) {12.    color: $color;13.}14.body {15.    @include test2(#fff);16.}17.18.// 多个参数19.@mixin test3($color: red, $fontSize: 12px) {20.    color: $color;21.    font-size: $fontSize;22.}23.24.body {25.    // 直接传值, 不可以打乱顺序26.    // @include test(blue, 18px);27.28.    // 通过键值对的方式,可以打乱传值的顺序29.    @include test3($fontSize: 18px, $color: blue);30.}31.32.// out css33./* line 6, test1 */34.body { color: red; }35.36./* line 14, test2*/37.body { color: #fff; }38.39./* line 24, test3*/40.body { color: blue; font-size: 18px; }
登入後複製

3.2 传递多值参数

  • 传递多值参数的时候,需要在参数后面加上三个点 ...表示这个参数可能含有多条属性,告诉sass不要区分逗号了,把传递的参数当做一个参数来解析.

1.// scss2.// 多值传递3.@mixin test4($shadow...) {4.    text-shadow: $shadow;5.    -webkit-text-shadow: $shadow;6.    -moz-text-shadow: $shadow;7.}8..text {9.    @include test4(1px 1px 10px red, 0 0 5px blue);10.}11.12.// out css13..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 传递内容

  • 传递内容就好比在方法中弄了个占位符, 当你调用的时候,所写的内容会直接放在占位符那里;

1.// scss2.@mixin style-for-iphone {3.    @media only screen and (min-width:320px) and (max-width:568px){4.       @content;5.    }6.}7.@include style-for-iphone {8.    height: 100px;9.    font-size: 12px;10.}11.12.// out css13.@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前面加一个 @符号,例如:

1.// scss2.@function double($width){3.    @return $width * 2;4.}5.6.body {7.    $color: rgba(255,0,0, .3);8.    color: $color;9.    width: 100%;10.    height: 100%;11.    p {12.        // 内置函数13.        color: darken($color, 0.5); // 加深14.        background-color: lighten($color, 0.9);// 变浅15.        z-index: str-length('aaaa'); // 字符串的长度16.        z-index: str-index('aaabddd','b');// 指定字符的索引的位置17.        width: double(5px);18.    }19.}20.21.// out css22.body { color: rgba(255, 0, 0, 0.3); width: 100%; height: 100%; }23.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.1 debug

  • @debug "这里的内容会在控制台输出"

5.2 warn

  • @warn "这里的内容会在控制台输出"

5.3 error

6. 条件语句及循环语句

6.1 条件语句

  • if的几种用法,如下

1.// scss2./*!if 的用法*/3.$width: 800;4.body {5.    // 跟三目运算符类似6.    color: if($width > 800, blue, red);7.}8.@if $width > 800 {9.    body {10.        color: red;11.    }12.} 13.@else if $width == 800 {14.    p {15.        color: blue;16.    }17.}18.@else {19.    body{ 20.        color: blue;21.    }22.}23.24./// out css25.body { color: red; }26.p { color: blue; }
登入後複製

6.2 循环语句

  • 语法1 for $i from 1 to 10
    • 此语句表示从1 到10,但是不包括10
  • 语法2 for $i from 1 through 10
    • 此语句表示从1到10,包括10
  • 下面代码是 through的用法,to的用法在这里就不演示了

1.// scss2.@for $i from 1 through 5 {3.    .span#{$i} {4.        width: 20% * $i; 5.    }6.}7.8.// out css9..span1 { width: 20%; }10..span2 { width: 40%; }11..span3 { width: 60%; }12..span4 { width: 80%; }13..span5 { width: 100%; }
登入後複製

6.3 while

  • 使用while 需要注意一下几点,

    • 第一: 变量要提前声明
    • 第二: while里面可以设置步长

1.// scss2.$j: 6;3.@while $j > 0 {4.    .p#{$j} {5.        width: 20% * $j;6.    }7.    $j: $j - 3;8.}9.10.// out css11..p6 { width: 120%; }12..p3 { width: 60%; }
登入後複製

7. each

7.1 常规遍历

1.// each 普通的用法2.$k: 1;3.$color: red, green, blue;4.@each $c in $color {5.    .div#{$k} {6.        color: $c;7.    }8.    $k: $k + 1;9.}10.11.// out css12.13..div1 { color: red; }14..div2 { color: green; }15..div3 { color: blue; }16.
登入後複製

7.2 遍历list

  • 这里出现的方式是以一个键一个值的形式出现的,如果是但数据的变量,那么可以在外边顶一个索引,内部执行加1 操作,也可以得到相应的效果.
  • $key表示键值, $color表示值

1.@each $key, $color in (default, green), (dange,blue), (error, red) {2.    .aler-#{$key} {3.        color: $color;4.    }5.}6.7.// out css8..aler-default { color: green; }9..aler-dange { color: blue; }10..aler-error { color: red; }
登入後複製

7.3 遍历map

  • 遍历map $key表示键值, $val表示值

1.//scss2.@each $key, $val in (default: green, dange: blue, error: red) {3.    .alert-#{key} {4.        color: $val;5.    }6.}7.8.// out css9..alert-key { color: green; }10..alert-key { color: blue; }11..alert-key { color: red; }12.
登入後複製

8. 小实例

  • 这里写了个小实例, 有兴趣的朋友可以看看,其实我也是抄的别人的实例,等会去写个小项目,orz我要睡觉了,明天写吧….好累的说,代码粘在下面了…

1.// scss2.@function buildContainer($num: 5) {3.    $map: (defaultValue: 0);4.    $rate: percentage(1 / $num); // percentage 求出百分比5.    @for $i from 1 through 5 {6.        $tempMap: (col#{$i} : $rate * $i);7.        $map: map-merge($map, $tempMap);8.    }9.    $map: map-remove($map, defaultValue);10.11.    @return $map;12.13.}14.@mixin buildContainer($num: 5) {15.    $map: buildContainer($num);16.    @each $key, $val in $map {17.        .#{$key} {18.            width: $val;19.        }20.    }21.}22.23.@include buildContainer();24.25.// out css26..col1 { width: 20%; }27..col2 { width: 40%; }28..col3 { width: 60%; }29..col4 { width: 80%; }30..col5 { width: 100%; }
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

&gt; gt;的目的是什麼 元素? &gt; gt;的目的是什麼 元素? Mar 21, 2025 pm 12:34 PM

本文討論了HTML&lt; Progress&gt;元素,其目的,樣式和與&lt; meter&gt;元素。主要重點是使用&lt; progress&gt;為了完成任務和LT;儀表&gt;對於stati

&lt; datalist&gt;的目的是什麼。 元素? &lt; datalist&gt;的目的是什麼。 元素? Mar 21, 2025 pm 12:33 PM

本文討論了html&lt; datalist&gt;元素,通過提供自動完整建議,改善用戶體驗並減少錯誤來增強表格。Character計數:159

&lt; meter&gt;的目的是什麼。 元素? &lt; meter&gt;的目的是什麼。 元素? Mar 21, 2025 pm 12:35 PM

本文討論了HTML&lt; meter&gt;元素,用於在一個範圍內顯示標量或分數值及其在Web開發中的常見應用。它區分了&lt; meter&gt;從&lt; progress&gt;和前

視口元標籤是什麼?為什麼對響應式設計很重要? 視口元標籤是什麼?為什麼對響應式設計很重要? Mar 20, 2025 pm 05:56 PM

本文討論了視口元標籤,這對於移動設備上的響應式Web設計至關重要。它解釋瞭如何正確使用確保最佳的內容縮放和用戶交互,而濫用可能會導致設計和可訪問性問題。

HTML容易為初學者學習嗎? HTML容易為初學者學習嗎? Apr 07, 2025 am 12:11 AM

HTML適合初學者學習,因為它簡單易學且能快速看到成果。 1)HTML的學習曲線平緩,易於上手。 2)只需掌握基本標籤即可開始創建網頁。 3)靈活性高,可與CSS和JavaScript結合使用。 4)豐富的學習資源和現代工具支持學習過程。

&lt; iframe&gt;的目的是什麼。 標籤?使用時的安全考慮是什麼? &lt; iframe&gt;的目的是什麼。 標籤?使用時的安全考慮是什麼? Mar 20, 2025 pm 06:05 PM

本文討論了&lt; iframe&gt;將外部內容嵌入網頁,其常見用途,安全風險以及諸如對象標籤和API等替代方案的目的。

HTML,CSS和JavaScript的角色:核心職責 HTML,CSS和JavaScript的角色:核心職責 Apr 08, 2025 pm 07:05 PM

HTML定義網頁結構,CSS負責樣式和佈局,JavaScript賦予動態交互。三者在網頁開發中各司其職,共同構建豐富多彩的網站。

HTML中起始標籤的示例是什麼? HTML中起始標籤的示例是什麼? Apr 06, 2025 am 12:04 AM

AnexampleOfAstartingTaginHtmlis,beginSaparagraph.startingTagSareEssentialInhtmlastheyInitiateEllements,defiteTheeTheErtypes,andarecrucialforsstructuringwebpages wepages webpages andConstructingthedom。

See all articles