【问题标题】:Change variable name in a loop在循环中更改变量名
【发布时间】:2015-10-01 16:49:47
【问题描述】:

我需要将变量name循环更改为treesballoons等以获得

.trees{ color:1;}
.balloons { color:2;}

但我得到了:

.trees{color:2}  
.name{color:1}

尝试了这个 mixin 但不起作用,我做错了什么?

@name:name;
.name(@index) when (@index = 2) {@name: balloons;}
.name(@index) when (@index = 1) {@name: trees;}
.name(@index) when (@index > 0) {
.@{name}{ color:@index; }
  .name(@index - 1)
}
.name(2);

【问题讨论】:

    标签: less mixins less-mixins


    【解决方案1】:

    看这个codepen,你必须使用所需名称的list,你可以通过它的索引获取列表的任何元素:

    @item: extract(@names, @index);
    

    【讨论】:

    • 解决这个问题的最佳方法:)
    • 感谢dajnz的帮助
    • 虽然将整个 sn-p 包含在答案中也很方便(毕竟它只是 a few lines of code)。
    猜你喜欢
    • 2016-06-02
    • 2019-01-31
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2019-10-08
    相关资源
    最近更新 更多