2014-09-22 47 views
0

我有顯示的背景圖像的一類:遞增背景圖像ID與薩斯

@media all and (min-width: 31.25em) { 
    .switch-image { 
     display:block; 
     background-image: url(/asset/img/img-1.png); 
     background-repeat: no-repeat; 
     background-position: center; 
    } 
} 

我的問題是,我有大約30的圖像(img-1.pngimg-30.png

如何能我省了不用寫上面30次。

我可以通過上述循環並增加panel-11所以panel-2

+0

你能舉一個HTML的小例子嗎?所有30個元素是否有相同的類(第一個元素將獲得img-2,第二個img-2 ...)或不同的類('panel-1','panel-2')? – nishantjr 2014-09-22 12:09:19

+0

@nishantjr - HTML的例子可以在這裏找到:http://scheckoverflow.com/questions/25972274/css-image-swapping-removing-duplicated-code/25972502?noredirect=1#comment40667507_25972502 – 2014-09-22 12:11:28

回答

1

您可以使用@for看:

@media all and (min-width: 31.25em) { 
    @for $i from 1 through 30 { 
    .switch-image-#{$i} { 
     display:block; 
     background-image: url(/asset/img/img-#{$i}.png); 
     background-repeat: no-repeat; 
     background-position: center; 
    } 
    } 
} 

希望它能幫助。

問候。

+1

'.switch-image- #{$ I}'? – nishantjr 2014-09-22 11:49:33

+0

@nishantjr - 如何使用您的建議影響HTML標記?請你能提供例子嗎? – 2014-09-22 11:52:41

+0

根據我對'SASS'的理解(以前從未使用過),這會爲'.switch-image'生成30個css聲明,只有最後一個'background-image'生效,所以不起作用。 [鏈接到文檔](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#_10) – nishantjr 2014-09-22 12:07:24