2017-01-13 74 views
2

我試圖用sass迭代製作一個循環。 這是我的SCSS代碼:在sass中迭代製作@for循環

@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,因爲我是一個新手。

+1

奇怪,當我把你的SASS代碼爲[http://www.sassmeister.com/](http://www.sassmeister.com/)它顯示了預期的結果。 – Nope

+0

似乎for循環在.slide選擇器之外。那是對的嗎? –

回答

1

假設輸出顯示@for循環放置在#slideshow選擇器中,您應該根據您的首選項移動.slide選擇器內的for循環,或將for循環中的.slide選擇器移動。

#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; 
      } 
     } 
    } 
}