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

11

以下的代码可以用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);
}
}

但这只会给所有的li都提供'../images/bgn.jpg'。

1个回答

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

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

seven-phases-max的回答很完整。但如果你和我一样在寻找更通用的内容,这可能会有所帮助:.applyToCol(@colNum...:nth-child(0n+@{colNum}) - Zachary Ryan Smith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接