为了账号安全,请及时绑定邮箱和手机立即绑定

Scss进阶篇1

标签:
Html/CSS

@if

@if 指令是一个 SassScript,它可以根据条件来处理样式块,如果条件为 true 返回一个样式块,反之 false 返回另一个样式块。在 Sass 中除了 @if 之,还可以配合 @else if 和 @else 一起使用。

假设要控制一个元素隐藏或显示,我们就可以定义一个混合宏,通过 @if...@else... 来判断传进参数的值来控制 display 的值。如下所示:

//SCSS@mixin blockOrHidden($boolean:true) {  @if $boolean {    @debug "$boolean is #{$boolean}";
    display: block;
  }  @else {      @debug "$boolean is #{$boolean}";
      display: none;
   }
}
.block {  @include blockOrHidden;
}
.hidden{  @include blockOrHidden(false);
}

编译出来的CSS:

.block {  display: block;
}.hidden {  display: none;
}

@for循环

在制作网格系统的时候,大家应该对 .col1~.col12 这样的印象较深。在 CSS 中你需要一个一个去书写,但在 Sass 中,可以使用 @for 循环来完成。在 Sass 的 @for 循环中有两种方式:

@for $i from <start> through <end>
@for $i from <start> to <end>
  • $i 表示变量

  • start 表示起始值

  • end 表示结束值

这两个的区别是关键字 through 表示包括 end 这个数,而 to 则不包括 end 这个数。
如下代码,先来个使用 through 关键字的例子:

@for $i from 1 through 3 {
  .item-#{$i} { width: 2em * $i; }}

编译出来的 CSS:

.item-1 {  width: 2em;
}.item-2 {  width: 4em;
}.item-3 {  width: 6em;
}

再来个 to 关键字的例子:

  @for $i from 1 to 3 {
  .item-#{$i} { width: 2em * $i; }}

编译出来的 CSS:

.item-1 {  width: 2em;
}.item-2 {  width: 4em;
}

@for应用在网格系统生成各个格子 class 的代码:

//SCSS $grid-prefix: span !default;
$grid-width: 60px !default;
$grid-gutter: 20px !default;

%grid {
  float: left;
  margin-left: $grid-gutter / 2;
  margin-right: $grid-gutter / 2;
}
@for $i from 1 through 12 {
  .#{$grid-prefix}#{$i}{
    width: $grid-width * $i + $grid-gutter * ($i - 1);
  @extend %grid;
  }  
}

编译出来的 CSS:

.span1, .span2, .span3, .span4, .span5, .span6, .span7, .span8, 
.span9, .span10, .span11, .span12 {  float: left;  margin-left: 10px;  margin-right: 10px;
}.span1 {  width: 60px;
}.span2 {  width: 140px;
}.span3 {  width: 220px;
}.span4 {  width: 300px;
}.span5 {  width: 380px;
}.span6 {  width: 460px;
}.span7 {  width: 540px;
}.span8 {   width: 620px;
}.span9 {  width: 700px;
}.span10 {  width: 780px;
}.span11 {  width: 860px;
}.span12 {  width: 940px;
}

@while循环

@while 指令也需要 SassScript 表达式(像其他指令一样),并且会生成不同的样式块,直到表达式值为 false 时停止循环。这个和 @for 指令很相似,只要 @while 后面的条件为 true 就会执行。
这里有一个 @while 指令的简单用例:

//SCSS$types: 4;
$type-width: 20px;
@while $types > 0 {
    .while-#{$types} {
        width: $type-width + $types;
    }
    $types: $types - 1;
}

编译出来的 CSS

.while-4 {  width: 24px;
}.while-3 {  width: 23px;
}.while-2 {  width: 22px;
}.while-1 {  width: 21px;
}

@each循环

@each 循环就是去遍历一个列表,然后从列表中取出对应的值。
@each 循环指令的形式:

@each $var in <list>

在下面的例子中你可以看到,var 就是一个变量名,<list> 是一个 SassScript 表达式,他将返回一个列表值。变量var 会在列表中做遍历,并且遍历出与 $var 对应的样式块。

这有一个 @each 指令的简单示例:

$list: adam john wynn mason kuroir;//$list 就是一个列表
@mixin author-images {
    @each $author in $list {
        .photo-#{$author} {
            background: url("/images/avatars/#{$author}.png") no-repeat;
        }
    }
}
.author-bio {
    @include author-images;
}

编译出 CSS:

.author-bio .photo-adam {  background: url("/images/avatars/adam.png") no-repeat; }.author-bio .photo-john {  background: url("/images/avatars/john.png") no-repeat; }.author-bio .photo-wynn {  background: url("/images/avatars/wynn.png") no-repeat; }.author-bio .photo-mason {  background: url("/images/avatars/mason.png") no-repeat; }.author-bio .photo-kuroir {  background: url("/images/avatars/kuroir.png") no-repeat; }

Sass的函数简介:

在 Sass 中除了可以定义变量,具有 @extend、%placeholder 和 mixins 等特性之外,还自备了一系列的函数功能。其主要包括:

  • 字符串函数

  • 数字函数

  • 列表函数

  • 颜色函数

  • Introspection 函数

  • 三元函数等

当然除了自备的函数功能之外,我们还可以根据自己的需求定义函数功能,常常称之为自定义函数。


字符串函数-unquote()函数

字符串函数顾名思意是用来处理字符串的函数。Sass 的字符串函数主要包括两个函数:

  • unquote($string):删除字符串中的引号;

  • quote($string):给字符串添加引号。

1、unquote()函数:
unquote() 函数主要是用来删除一个字符串中的引号,如果这个字符串没有带有引号,将返回原始的字符串。简单的使用终端来测试这个函数的运行结果:

//SCSS.test1 {
    content:  unquote('Hello Sass!') ;
}
.test2 {
    content: unquote("'Hello Sass!");
}
.test3 {
    content: unquote("I'm Web Designer");
}
.test4 {
    content: unquote("'Hello Sass!'");
}
.test5 {
    content: unquote('"Hello Sass!"');
}
.test6 {
    content: unquote(Hello Sass);
}

编译后的 css 代码:

//CSS.test1 {
  content: Hello Sass!; }
.test2 {
  content: 'Hello Sass!; }
.test3 {
  content: I'm Web Designer; }
.test4 {
  content: 'Hello Sass!'; }
.test5 {
  content: "Hello Sass!"; }
.test6 {
  content: Hello Sass; }

注意:从测试的效果中可以看出,unquote( ) 函数只能删除字符串最前和最后的引号(双引号或单引号),而无法删除字符串中间的引号。如果字符没有带引号,返回的将是字符串本身。

quote() 函数刚好与 unquote() 函数功能相反,主要用来给字符串添加引号。如果字符串,自身带有引号会统一换成双引号 ""。如:

//SCSS.test1 {
    content:  quote('Hello Sass!');
}
.test2 {
    content: quote("Hello Sass!");
}
.test3 {
    content: quote(ImWebDesigner);
}
.test4 {
    content: quote(' ');
}

编译出来的 css 代码:

//CSS.test1 {
  content: "Hello Sass!";
}
.test2 {
  content: "Hello Sass!";
}
.test3 {
  content: "ImWebDesigner";
}
.test4 {
  content: "";
}

使用 quote() 函数只能给字符串增加双引号,而且字符串中间有单引号或者空格时,需要用单引号或双引号括起,否则编译的时候将会报错。

.test1 {    content:  quote(Hello Sass);
}

这样使用,编译器马上会报错:

error style.scss (Line 13: $string: ("Hello""Sass") is not a string for `quote')

解决方案就是去掉空格,或者加上引号:

.test1 {content:  quote(HelloSass);
}.test1 {    content:  quote("Hello Sass");
}

同时 quote() 碰到特殊符号,比如: !、?、> 等,除中折号 - 和 下划线_ 都需要使用双引号括起,否则编译器在进行编译的时候同样会报错:

error style.scss (Line 13: Invalid CSS after "...quote(HelloSass": expected ")", was "!);")
error style.scss (Line 16: Invalid CSS after "...t:  quote(Hello": expected ")", was “?);")

字符串函数-To-upper-case()、To-lower-case()

1、To-upper-case()
To-upper-case() 函数将字符串小写字母转换成大写字母。如:

//SCSS.test {
  text: to-upper-case(aaaaa);
  text: to-upper-case(aA-aAAA-aaa);
}

编译出来的 css 代码:

//CSS
.test {
  text: AAAAA;
  text: AA-AAAA-AAA;
}

2、To-lower-case()

To-lower-case() 函数 与 To-upper-case() 刚好相反,将字符串转换成小写字母:

//SCSS.test {
text: to-lower-case(AAAAA);
text: to-lower-case(aA-aAAA-aaa);
}

编译出来的 css 代码:

//CSS
.test {
  text: aaaaa;
  text: aa-aaaa-aaa;
}

数字函数-percentage()

percentage()函数主要是将一个不带单位的数字转换成百分比形式:

.footer{    width : percentage(.2)
}

编译后的 css 代码:

.footer{    width : 20%}

数字函数-round()函数

round() 函数可以将一个数四舍五入为一个最接近的整数:

.footer {   width:round(12.3px)
}

编译后的 css 代码:

.footer {  width: 12px;
}

数字函数-ceil()函数

ceil() 函数将一个数转换成最接近于自己的整数,会将一个大于自身的任何小数转换成大于本身 1 的整数。也就是只做入,不做舍的计算:

.footer {   width:ceil(12.3px);
}

编译后的 css 代码:

.footer {  width: 13px;
}

数字函数-floor()函数

floor() 函数刚好与 ceil() 函数功能相反,其主要将一个数去除其小数部分,并且不做任何的进位。也就是只做舍,不做入的计算:

.footer {   width:floor(12.3px);
}

编译后的 css 代码:

.footer {  width: 12px;
}

数字函数-abs()函数

abs( ) 函数会返回一个数的绝对值。

.footer {   width:abs(-12.3px);
}

编译后的 css 代码:

  .footer {      width: 12.3px;
}

数字函数-min()函数、max()函数

min()函数

min() 函数功能主要是在多个数之中找到最小的一个,这个函数可以设置任意多个参数:

>> min(1,2,1%,3,300%)1%>> min(1px,2,3px)1px>> min(1em,2em,6em)1em

不过在 min() 函数中同时出现两种不同类型的单位,将会报错误信息:

>> min(1px,1em)SyntaxError: Incompatible units: 'em' and 'px'.
max()函数

max() 函数和 min() 函数一样,不同的是,max() 函数用来获取一系列数中的最大那个值:

>> max(1,5)5>> max(1px,5px)5px

同样的,如果在 max() 函数中有不同单位,将会报错:

>> max(1,3px,5%,6)SyntaxError: Incompatible units: '%' and ‘px'.

数字函数-random()函数

random() 函数是用来获取一个随机数:

 >> random()0.03886>> random()0.66527>> random()0.8125>> random()0.26839>> random()0.85063



作者:我待前端如初恋
链接:https://www.jianshu.com/p/279d0d030373


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消