sass2:
ass学习笔记2
今天介绍sass在重用代码时最具威力的两个功能。一个是嵌套(Nesting),一个混合(Mixin)。
我们在写CSS通过需要多个后代选择器组合到一起才能定位到目标元素上,而这定义过程,此元素的父元素与其他选择器都有许多相同的部分,嵌套就是把这些相同的部分放到某一层花括号里,然后通过花括号的层层缩进堆砌,找到它们的共性与特性。
| @charset "utf-8";//必须设置了这个才能编译有中文的注释nav {    ul {        margin: 0;        padding: 0;        list-style: none;    }    li { display: inline-block; }    a {        display: block;        padding: 6px12px;        text-decoration: none;    }}//------------------------------nav ul {  margin: 0;  padding: 0;  list-style: none; }nav li {  display: inline-block; }nav a {  display: block;  padding: 6px12px;  text-decoration: none; } | 
当某一天,我们要兼容IE678等低级浏览器,发现它直接用nav不行时,需要改成.nav,我们只要改一处就行了,这大大提高维护性。
sass的嵌套包括两种:一种是选择器嵌套;另一种是属性嵌套。
选择器嵌套是指在一个选择器里面能出现多重花括号,里面的每一重花括号前的选择器会在编译后,直接在前面加一个空白(后代选择器)跟在前方选择器的后面。 如果你不想它们之间出现后代选择器,可以在选择器的前面加一个&进行处理。
| @charset "utf-8";//必须设置了这个才能编译有中文的注释#top_nav{    line-height: 40px;    text-transform: capitalize;    background-color:#333;    li{        float:left;    }    a{        display: block;        padding: 010px;        color: #fff;        &:hover{            color:#ddd;        }    }}//------------------------------nav ul {  margin: 0;  padding: 0;  list-style: none; }nav li {  display: inline-block; }nav a {  display: block;  padding: 6px12px;  text-decoration: none; } | 
属性嵌套,与前面的语法差不多,不同的是前面的选择器跟着一个冒号,那么编译后,它就像它转换为一个横扛与后面的选择器连接成一个单词。
| @charset "utf-8";//必须设置了这个才能编译有中文的注释.fakeshadow {    border: {        style: solid;        left: {            width: 4px;            color: #888;        }        right: {            width: 2px;            color: #ccc;        }    }}//------------------------------.fakeshadow {  border-style: solid;  border-left-width: 4px;  border-left-color: #888;  border-right-width: 2px;  border-right-color: #ccc; } | 
sass3.3还添加了一个@at-root,用于跳出嵌套的,但我觉得现在说会破坏文章的流程,跳过。
本文的第二个重点是@mixin,其实就相当于JS的函数,并且在传参方面,已经拥有es6、es7的一些新功能了。
为了与内置方法的风格保持一致,建议大家都使用ruby风格(全部小写,并通过横杠连接)。 它的参数与变量名要求一致,需要以$打头,多个参数与逗号分开。如果参数有默认值,后面跟冒号再跟默认值。 如果想在其他地方使用这个函数,需要通过@include关键字来声明一下。
| @charset "utf-8";//必须设置了这个才能编译有中文的注释@mixin center-block{//没有参数    margin-left:auto;    margin-right:auto;}.demo{    @include center-block();//小括号可以不写,但建议大家都写}@mixin opacity($opacity:50) {//有参数,并且有默认值  opacity: $opacity / 100;  filter: alpha(opacity=$opacity);}.opacity{  @include opacity(); //使用默认值}.opacity-80{  @include opacity(80); //使用传参}//------------------------------.demo {  margin-left: auto;  margin-right: auto; }.opacity {  opacity: 0.5;  filter: alpha(opacity=50); }.opacity-80{  opacity: 0.8;  filter: alpha(opacity=80); } | 
我们用@mixin搞一个兼容IE67的浮动函数练练手!
| $lte7:true;@mixin float($float:left) {  float: $float;  @if $lte7{    display: inline;  }} | 
再来一个创建小三角形的函数
| @charset "utf-8";//必须设置了这个才能编译有中文的注释@mixin triangle($direction, $size, $borderColor ) {  content:"";  height: 0;  width: 0;  @if $direction == top{    border-bottom:$size solid$borderColor;    border-left:$size dashedtransparent;    border-right:$size dashedtransparent;  }   @else if $direction == right{    border-left:$size solid$borderColor;    border-top:$size dashedtransparent;    border-bottom:$size dashedtransparent;  }   @else if $direction == bottom{    border-top:$size solid$borderColor;    border-left:$size dashedtransparent;    border-right:$size dashedtransparent;  }   @else if $direction == left{    border-right:$size solid$borderColor;    border-top:$size dashedtransparent;    border-bottom:$size dashedtransparent;  }}.top-arrow {  @include triangle(top, 1px, red)}//------------------------------.top-arrow {  content: "";  height: 0;  width: 0;  border-bottom: 1pxsolidred;  border-left: 1pxdashedtransparent;  border-right: 1pxdashedtransparent; } | 
@content传参机制,它相当于在@include 调用语句后添加了一对括号,括号里的内容当作额外的参数来替换 @content所在的位置。 此特性在sass3.2.0中引入,可以用来解决css3的@media等带来的问题。
| @charset "utf-8";//必须设置了这个才能编译有中文的注释@mixin max-screen($res){//参数一  @media only screenand ( max-width: $res )  {    @content;//参数二  }}@include max-screen(480px) {//传参一  body { color: red}//传参二}//------------------------------@media only screenand (max-width: 480px) {  body {    color: red; } } | 
此外,sass还有占位符及与它配套使用的@extend,它们的用法与@mixin, @include相差无几,以后再说。
@function函数,此函数能对样式的规则的属性值进行更精致的计算,并且调用时不需要@include,相当的方便。许多内置函数就是由它创建的。
| //颜色处理  lighten(#cc3, 10%) // #d6d65c  darken(#cc3, 10%) // #a3a329  grayscale(#cc3) // #808080  complement(#cc3) // #33c    rgba(#102030, 0.5) => rgba(16, 32, 48, 0.5)    rgba(blue, 0.2)    => rgba(0, 0, 255, 0.2)//这个函数将一个颜色格式化成ie滤镜使用,在做css3使用滤镜兼容的时候用得上    ie-hex-str(#abc) => #FFAABBCC    ie-hex-str(#3322BB) => #FF3322BB    ie-hex-str(rgba(0, 255, 0, 0.5)) =>#8000FF00 | 
为数组添加更多方法
| //定义first()函数,获取列表中的第一个列表项@function list-first($list){    @return nth($list,1);}//定义last()函数,获取列表中的最后一个列表项@function list-last($list){    @return nth($list,length($list));}//移除数组某个元素@function remove($list, $value, $recursive: false) {  $result: ();  @for $i from 1through length($list) {    @if type-of(nth($list, $i)) == list and $recursive {      $result: append($result, remove(nth($list, $i), $value, $recursive));    }    @else if nth($list, $i) != $value {      $result: append($result, nth($list, $i));    }  }  @return $result;}$list: a, b z, c, z, d, z, e, f;$new-list: remove($list, z);       // a, b z, c, d, e, f;$new-list: remove($list, z, true); // a, b, c, d, e, f//在某一位置上删除元素@function remove-nth($list, $index) {  $result: null;  @if type-of($index) != number {    @warn "$index: #{quote($index)} is not a number for `remove-nth`.";  }  @else if $index == 0{    @warn "List index 0 must be a non-zero integer for `remove-nth`.";  }  @else if abs($index) > length($list) {    @warn "List index is #{$index} but list is only #{length($list)} item long for `remove-nth`.";  }  @else {    $result: ();    $index: if($index < 0, length($list) + $index + 1, $index);      @for $i from 1through length($list) {      @if $i != $index {        $result: append($result, nth($list, $i));      }    }  }  @return $result;}$list: a, b, z, c, d, e, f;$new-list: remove-nth($list,   3); // a, b, c, d, e, f$new-list: remove-nth($list,   0); // error$new-list: remove-nth($list,  -2); // a, b, z, c, d, f$new-list: remove-nth($list, -10); // error$new-list: remove-nth($list, 100); // error$new-list: remove-nth($list, zog); // error//对数组进行切片操作@function slice($list, $start: 1, $end: length($list)) {  $result: null;  @if type-of($start) != number or type-of($end) != number {    @warn "Either $start or $end are not a number for `slice`.";  }  @else if $start > $end {    @warn "The start index has to be lesser than or equals to the end index for `slice`.";  }  @else if $start < 1or $end < 1{    @warn "List indexes must be non-zero integers for `slice`.";  }  @else if $start > length($list) {    @warn "List index is #{$start} but list is only #{length($list)} item long for `slice`.";  }  @else if $end > length($list) {    @warn "List index is #{$end} but list is only #{length($list)} item long for `slice`.";  }  @else {    $result: ();    @for $i from $start through $end {      $result: append($result, nth($list, $i));    }  }  @return $result;}}$list: a, b, c, d, e, f;$new-list: slice($list, 3, 5);   // c, d, e$new-list: slice($list, 4, 4);   // d$new-list: slice($list, 5, 3);   // error$new-list: slice($list, -1, 10); // error//反转数组@function reverse($list, $recursive: false) {   $result: ();   @for $i from length($list)*-1through -1{      @if type-of(nth($list, abs($i))) == list and $recursive {        $result: append($result, reverse(nth($list, abs($i)), $recursive));            }      @else {        $result: append($result, nth($list, abs($i)));      }   }   @return $result;}$list: a, b, c d e, f, g, h;$new-list: reverse($list);       // h, g, f, c d e, b, a$new-list: reverse($list, true); // h, g, f, e d c, b, a//函数全部来自 http://hugogiraudel.com/2013/08/08/advanced-sass-list-functions/ | 
我们再创建一个页面看看unshift方法的效果
| <!DOCTYPEhtml><html>    <head>        <title>TODO supply a title</title>        <metacharset="UTF-8">        <metaname="viewport" content="width=device-width">    <linkrel="stylesheet" type="text/css" href="css/index.css">    </head>    <body>        <divclass="text">司徒正美</div>        <ahref="http://www.cnblogs.com/rubylouvre">这是链接</a>    </body></html> | 
| @charset "utf-8";//必须设置了这个才能编译有中文的注释$list: b, c, d, e, f;@function unshift($list, $value) {    @return join($value, $list);}$newlist: unshift($list, a);               // a, b, c, d, e, fbody{    background: blanchedalmond;}.text{    &:after{        color:red;        display:block;        border:1pxsolidred;        content: "#{$newlist}";    }}//------------------------------body {  background: blanchedalmond; }.text:after {  color: red;  display: block;  border: 1pxsolidred;  content: "a, b, c, d, e, f"; } | 
我们再定义一个将rem转换为px的函数:
| @charset "utf-8";//必须设置了这个才能编译有中文的注释$pixelBase : 16; $px-only:true;@function parseInt($n) {    @return $n / ($n * 0+ 1); }@function rem2px($values){     $list: ();    @each $value in $values { //遍历数组        $unit : unit($value); //取得其单位        $val  : parseInt($value); //取得其值        @if ($px-only) and ($unit == 'rem') {             $list: append($list, ($val * $pixelBase) + px);         }        @else if($unit == 'px') or ($unit == 'rem'){             $list: append($list, $value);         }        @else {            @warn 'There is no unit conversion for #{$unit}';         }    }    @return $list;}body * {    margin:rem2px(1rem 2rem 20px3rem);    padding-bottom:rem2px(0.25rem);    font-size:rem2px(0.875rem);}//------------------------------body * {  margin: 16px32px20px48px;  padding-bottom: 4px;  font-size: 14px; } | 
有了函数,sass就真正算是一门编程语言了!~
Ruby's Louvre,司徒正美的博客。
sass2:的更多相关文章
- 使用gulp解决外部编辑器修改Eclipse文件延迟更新的问题
		本人前端用惯了Hbuilder,修改了eclipse项目中的文件后,由于是外部编辑器修改过的,eclipse不会自动部署更新,一般按F5刷新项目,或者在 preferences > genera ... 
- 使用gulp解决外部编辑器修改Eclipse文件延迟刷新
		本人前端用惯了Hbuilder,修改了eclipse项目中的文件后,由于是外部编辑器修改过的,eclipse不会自动部署更新,一般按F5刷新项目,或者在 preferences > genera ... 
- 浅谈stylus与sass的对比
		all we konw , 这两个都是css的预编译工具,但虽然都是编译工具,但还是存在差别的,下面来讲讲其中的区别 1.变量 sass定义变量是以这种形式进行定义的$xxx:10;而stylus的定 ... 
随机推荐
- php截取中文字符串乱码问题
			一般情况下说到截取字符串我们都会想到substr 然而substr对英文字符串有不错的效果,但是中文可能就会报出各种各样的问题: 所以,我们要采用mb库里面的substr,也就是mb_substr() ... 
- windows+caffe(六)——convert.bat
			convert.bat的格式为 convert_imageset.exe的位置+空格+FLAGS+空格+图片所在的位置+空格+你生成的list的位置+空格+将要生成的db格式要保存的位置 建议都使用绝 ... 
- [课程设计]任务进度条&开发日志目录
			任务进度条&开发日志目录 周期 时间 任务 Sprint One 11.14 ● Scrum团队分工及明确任务1.0 Sprint One 11.15 ● Scr ... 
- PetGenie
			大概六.七年前当我还在学 Asphyre 的时候,有看过一个以之编写的类似对对碰的“宠物对对碰”小游戏,虽然很简单,但我当时还是小小的沉溺过数个小时.而不久前,在闲逛论坛时无意看到了个以 FireMo ... 
- 【jQuery】【转】jQuery中的trigger和triggerHandler区别
			trigger(event, [data]) 在每一个匹配的元素上触发某类事件. 这个函数也会导致浏览器同名的默认行为的执行.比如,如果用trigger()触发一个'submit',则同样会导致浏览器 ... 
- Ubuntu学习小结(一)
			这段时间,抽空研究了一下Ubuntu,虽然也有过到目前为止使用计算机最作死的经历,但目前已经学会了一些最基本的操作.在这里简单的记录一下,算是吸取的教训,供其他人借鉴. 1.装Ubuntu系统.装Ub ... 
- TCP/IP基础概念及通信过程举例
			TCP/IP基础概念及通信过程举例 出现 上个世纪60年代,由于中央集中式网络的容灾性较弱,以美国国防部为中心的一家组织研究出分组交换网络.后来为了验证分组交换技术的实用性,ARPANET出现了,并且 ... 
- 创建EF数据模型
			最后的应用程序,看上去就像下边这样: 创建数据模型,你将从以下三个类开始: 在Models文件夹,新建以下类: using System; using System.Collections.Gener ... 
- [Python]简易terminal歌词滚动播放器
			整合了网易云的一些API,想写一个terminal版的音乐播放器,但是还没有想好写成什么样子. 暂时写了一个必须又的功能:带歌词滚动的播放器,用了pygame里的mixer来播放音乐. 准备有时间的时 ... 
- The communication of Linux Server and Localtion
			当用Secure CRT远程登录服务器时,若建立本地与服务器间文件自由传输的机制,我们就可以实现远程办公.具体方法如下: 1. 确定远程服务器的IP.可以通过Secure CRT进行远程登录. 2.在 ... 
