【问题标题】:LESS - use nth-child variable in stringLESS - 在字符串中使用第 n 个子变量
【发布时间】:2013-12-20 14:23:59
【问题描述】:

肯定有办法在 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'。

【问题讨论】:

    标签: less css-selectors


    【解决方案1】:
    #bg-slider {
        li {
            .bkg(1);
            .bkg(2);
            .bkg(3);
        }
    
        .bkg(@i) {
            &:nth-child(@{i}) {
                background: url('../images/bg@{i}.jpg');
            }
        }
    }
    

    【讨论】:

    • 七相最大的答案是完整的。但是,如果您像我一样正在寻找更通用的东西,这可能会有所帮助:.applyToCol(@colNum...:nth-child(0n+@{colNum})
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多