Sass: Change color with @for loop

前端 未结 3 708
独厮守ぢ
独厮守ぢ 2021-01-03 01:49

I try to darken a variable number of divs like that \"enter with following code:



        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-03 02:48

    I created this example based on your mixin:

    @mixin color-divs ($count, $baseName, $startcolor) {
        $loop_color: $startcolor;
        @for $i from 0 through $count {
            $loop_color: darken($loop_color, 9%);
            .#{$baseName}-#{$i} { background-color: $loop_color; }
        }
    }
    
    div {
        width: 100px;
        height: 100px;
        float: left;
    }
    
    @include color-divs(6,'div',#faa)
    

    Used with the following markup:

    Output: http://jsfiddle.net/jdtvF/

    http://uk.omg.li/P0dF/by%20default%202013-05-16%20at%2010.10.43.png

    div {
      width: 100px;
      height: 100px;
      float: left; }
    
    .div-0 {
      background-color: #ff7c7c; }
    
    .div-1 {
      background-color: #ff4e4e; }
    
    .div-2 {
      background-color: #ff2020; }
    
    .div-3 {
      background-color: #f10000; }
    
    .div-4 {
      background-color: #c30000; }
    
    .div-5 {
      background-color: #960000; }
    
    .div-6 {
      background-color: #680000; }
    

提交回复
热议问题