简体   繁体   English

创建@for或@each以使用scss自动化样式

[英]Create a @for or @each to automate styles with scss

The output I am trying to achieve is 我想要实现的输出是

.gray-1 {
  height: 10px;
}
.gray-2 {
  height: 20px;
}
.gray-3 {
  height: 30px;
}
.gray-4 {
  height: 40px;
}
.gray-5 {
  height: 50px;
}
.gray-6 {
  height: 60px;
}
.gray-7 {
  height: 70px;
}
.gray-8 {
  height: 80px;
}
.gray-9 {
  height: 90px;
}
.red-1 {
  height: 10px;
}
this will end at 9 and then .blue-1 ++ will start
etc....

below is the current each scss function i am playing with but it outputs 下面是我正在玩的每个scss函数,但它输出

.gray red blue-1 {
  height: 10px;
}

Can i make it pass by gray 1-9 and then red 1-9 then finally blue 1-9. 我可以通过灰色1-9然后红色1-9然后最后蓝色1-9。 also is it necessary to do the height list? 还有必要做高度清单吗? can I just do n+10 up to 90 in someway? 我可以在某种程度上做n + 10到90吗?

$height-list: 10 20 30 40 50 60 70 80 90;
$color-var: gray red blue;

@each $current-color in $height-list {
    $i: index($height-list, $current-color);
  .#{$color-var}-#{$i} { 
        height: #{$current-color}px;
    }
}

codepen codepen

This option works perfect. 这个选项很完美。 all you need to do is use two loops for your two multi vars. 你需要做的就是为你的两个多变量使用两个循环。

scss: SCSS:

$height-list: 10, 20, 30, 40, 50, 60, 70, 80, 90;
$color-var: gray, red, blue;

    @for $c from 1 through length($color-var) {
      @for $h from 1 through length($height-list) {
        .#{nth($color-var, $c)}-#{$h} {
          height: nth($height-list, $h) + px;
        }
      }
    }

css output: css输出:

.gray-1 {
  height: 10px; }

.gray-2 {
  height: 20px; }

.gray-3 {
  height: 30px; }

.gray-4 {
  height: 40px; }

.gray-5 {
  height: 50px; }

.gray-6 {
  height: 60px; }

.gray-7 {
  height: 70px; }

.gray-8 {
  height: 80px; }

.gray-9 {
  height: 90px; }

.red-1 {
  height: 10px; }

.red-2 {
  height: 20px; }

.red-3 {
  height: 30px; }

.red-4 {
  height: 40px; }

.red-5 {
  height: 50px; }

.red-6 {
  height: 60px; }

.red-7 {
  height: 70px; }

.red-8 {
  height: 80px; }

.red-9 {
  height: 90px; }

.blue-1 {
  height: 10px; }

.blue-2 {
  height: 20px; }

.blue-3 {
  height: 30px; }

.blue-4 {
  height: 40px; }

.blue-5 {
  height: 50px; }

.blue-6 {
  height: 60px; }

.blue-7 {
  height: 70px; }

.blue-8 {
  height: 80px; }

.blue-9 {
  height: 90px; }

/*# sourceMappingURL=style.css.map */

Here is my solution to this 这是我的解决方案

$height-list: 10 20 30 40 50 60 70 80 90;
$gray-color:gray;
$red-color:red;
$blue-color:blue;
@each $current-color in $height-list {
    $i: index($height-list, $current-color);

  .#{$gray-color}-#{$i} { 
        height: #{$current-color}px;
    }
 .#{$red-color}-#{$i} { 
        height: #{$current-color}px;
    }
.#{$blue-color}-#{$i} { 
        height: #{$current-color}px;
    }
}

Hope this helps 希望这可以帮助

You can also do to avoid having multiple variables as this check this 您还可以避免使用多个变量,因此请检查此项

$height-list: 10 20 30 40 50 60 70 80 90;
$colors: gray,red,blue;
@each $current-color in $colors{
  $color:index($colors,$current-color);
  @each $current-height in $height-list {
      $i: index($height-list, $current-height);
   .#{$current-color}-#{$i} { 
        height: #{$current-color}px;
    }
  }
}

If you've got two arrays, you probably need two @each loops. 如果你有两个数组,你可能需要两个@each循环。

$height-list: 10 20 30 40 50 60 70 80 90;
$color-var: gray red blue;

@each $color in $color-var{
  @each $height in $height-list{
    $i: index($height-list, $height)
    .#{$color-var}-#{$i} {
      height: #{$height}px;  
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM