Sass - 循环遍历nth-child(#{$ i})定位的数字数组

时间:2017-06-15 19:52:50

标签: loops sass

我创建一个mixin来隐藏从include语句发送的第n个子值的列(th和td)。在我的代码中缺少什么,或者更好的是,这是最好的方法,即使纠正了工作?

包含声明:

$hidden-columns: (2, 3, 6, 9, 17);
@include hide-columns($hidden-columns);

密新

@mixin hide-columns($columns) {
  @for $i from 0 to length($columns) {
    th:nth-child(#{$columns[ $i ]}),
    td:nth-child(#{$columns[ $i ]}) {
      display: none;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我最终使用@each而不是循环(下面)但是,如果有人关心发布我仍然希望知道原始帖子中循环的正确语法,我会将其标记为答案。< / p>

@mixin hide-columns($columns) {
  @each $col in $columns {
    th:nth-child(#{$col}),
    td:nth-child(#{$col}) {
      display: none;
    }
  }
}