css - 在sass中使用迭代制作@for循环

标签 css sass

我正在尝试使用 sass 迭代创建一个循环。 这是我的 scss 代码:

@for $i from 1 through 4{
  &:nthchild(#{$i}){
    animation: xfade 24s 24s - $i * 6 infinite;
  }
}

这是我的输出:

#slideshow:nthchild($i) {
  -webkit-animation: xfade 24s 23s infinite;
  animation: xfade 24s 23s infinite;
}

#slideshow:nthchild($i) {
  -webkit-animation: xfade 24s 22s infinite;
  animation: xfade 24s 22s infinite;
}

#slideshow:nthchild($i) {
  -webkit-animation: xfade 24s 21s infinite;
  animation: xfade 24s 21s infinite;
}

#slideshow:nthchild($i) {
  -webkit-animation: xfade 24s 20s infinite;
  animation: xfade 24s 20s infinite;
}

这是我想要的结果:

#slideshow .slide:nth-child(1) {
-webkit-animation: xfade 24s 18s infinite;
animation: xfade 24s 18s infinite;
}
#slideshow .slide:nth-child(2) {
-webkit-animation: xfade 24s 12s infinite;
animation: xfade 24s 12s infinite;
}
#slideshow .slide:nth-child(3) {
-webkit-animation: xfade 24s 6s infinite;
animation: xfade 24s 6s infinite;
}
#slideshow .slide:nth-child(4) {
-webkit-animation: xfade 24s 0s infinite;
animation: xfade 24s 0s infinite;
}

我没有在网上找到任何清楚说明如何正确编写@for 循环的答案。我在旧的 .sass 中找到了一堆,但我不知道如何将其转换为 .scss,因为我是新手。

最佳答案

假设输出显示 @for 循环位于 #slideshow 选择器内,您应该将 for 循环移动到 .slide 选择器内,或者将 .slide 选择器移动到 for 循环内,具体取决于您的偏好:

#slideshow {
    @for $i from 1 through 4{
      & .slide:nthchild(#{$i}){
        animation: xfade 24s 24s - $i * 6 infinite;
      }
    }
}

#slideshow {
    .slide {
        @for $i from 1 through 4{
          &:nthchild(#{$i}){
            animation: xfade 24s 24s - $i * 6 infinite;
          }
        }
    }
}

关于css - 在sass中使用迭代制作@for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41631643/

相关文章:

javascript - Ukit 模态阻止关闭退出按钮

html - 粘性页脚在横向方向中断

css - 移动端网站宽度发生变化

css - 带有整数和小数的 Sass 控制指令 @for

css - React 混合单元错误 : Can't calculate vw and px together in css calc() function

html - 投资组合页面 Bootstrap 部分

html - 对齐表格中文本和图像的位置

javascript - 如何通过单击一个来选择两个表格单元格?

css - 为什么我的列表项缩略图是这样显示的?

css - 径向渐变 CSS 原点位于顶部,而不是中心