LESS - use nth-child variable in string

风格不统一 提交于 2019-12-05 17:14:25

问题


Surely there's a way to rewrite the following in 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');
}

}

I've tried:

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

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

But that just gives '../images/bgn.jpg' for all li's.


回答1:


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

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


来源:https://stackoverflow.com/questions/20369614/less-use-nth-child-variable-in-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!