LESS - 在字符串中使用nth-child变量

时间:2013-12-04 07:34:22

标签: less css-selectors

当然有一种方法可以在LESS重写以下内容?

#bg-slider{

li:nth-child(1){
    background:url('../images/bg1.jpg');
}

li:nth-child(2){
    background:url('../images/bg2.jpg');
}

li:nth-child(3){
    background:url('../images/bg3.jpg');
}

}

我试过了:

.bg-image (@slide) {
  background:url('../images/bg@{slide}.jpg');
}

#bg-slider{
li:nth-child(n){
    .bg-image(n);
}
}

但是这只是给所有李的'../images/bgn.jpg'。

1 个答案:

答案 0 :(得分:16)

#bg-slider {
    li {
        .bkg(1);
        .bkg(2);
        .bkg(3);
    }

    .bkg(@i) {
        &:nth-child(@{i}) {
            background: url('../images/bg@{i}.jpg');
        }
    }
}