zoukankan      html  css  js  c++  java
  • sass进阶篇总结一

    一、@if 指令:

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

    @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 循环

    在 Sass 的 @for 循环中有两种方式:

    @for $i from <start> through <end>
    @for $i from <start> to <end>
    

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

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

    //SCSS 代码:
    @for $i from 1 through 3 {
      .item-#{$i} {  2em * $i; }
    }
    
    //编译出来的css:
    .item-1 {  2em;}
    .item-2 {  4em;}
    .item-3 {  6em;}
    

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

    //SCSS 
    $grid-prefix: span !default;
    $grid- 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}{
         $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 {
       60px;
    }
    
    .span2 {
       140px;
    }
    
    .span3 {
       220px;
    }
    
    .span4 {
       300px;
    }
    
    .span5 {
       380px;
    }
    
    .span6 {
       460px;
    }
    
    .span7 {
       540px;
    }
    
    .span8 {
       620px;
    }
    
    .span9 {
       700px;
    }
    
    .span10 {
       780px;
    }
    
    .span11 {
       860px;
    }
    
    .span12 {
       940px;
    }
    

    三、@while 循环

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

    这里有一个 @while 指令的简单用例:

    //SCSS
    $types: 4;
    $type- 20px;
    
    @while $types > 0 {
        .while-#{$types} {
             $type-width + $types;
        }
        $types: $types - 1;
    }
    
    //编译出来的 CSS
    .while-4 {
       24px;
    }
    
    .while-3 {
       23px;
    }
    
    .while-2 {
       22px;
    }
    
    .while-1 {
       21px;
    }
    

    四、@each 循环

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

    @each 循环指令的形式:

    @each $var in <list>
    

    @each 指令的简单示例:

    //SCSS
    $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; }
    

      

      

     

     

  • 相关阅读:
    jdbc连接2(不可以注入)
    大白dmeo (转的)
    管家婆系统
    RMQ求最值
    Codeforces 837E Vasya's Function 数论 找规律
    Codeforces 837D
    poj 1655 找树的重心
    HDU 4055 Number String 计数DP
    Summer training round2 #7 (Training #23)
    Summer training round2 #6 (Training #22)
  • 原文地址:https://www.cnblogs.com/kt520/p/5710907.html
Copyright © 2011-2022 走看看