Dynamic Variable Names in LESS CSS

别等时光非礼了梦想. 提交于 2020-01-09 10:59:09

问题


I have been trying this for a while, looking at other answers, but to no avail. Hopefully someone can help me.

I am trying to generate some dynamic variable names within a mixin.

The variables are already defined:

@horizontal-default-color: #fff;
@horizontal-inverse-color: #000;

etc..

The mixin I want would be something like this:

.horizontal-variant(@variant) {
    color: @{horizontal-@{@variant}-color}
}

And the result I am expecting, when called:

.horizontal-default{
   .horizontal-variant(~"default");
}
.horizontal-inverse{
   .horizontal-variant(~"inverse");
}

is

.horizontal-default {color: #fff}
.horizontal-inverse {color: #000}

Unfortunately I run into errors every time.

I know this can be done, I have seen it being used in Font Awesome LESS where @{fa-css-prefix} is defined in variables. I am just having trouble transporting the same solution in my project.

You can try testing the code at http://lesstester.com/


回答1:


You can use Variable Names. And I've tested the code at http://lesstester.com/, it works.

@horizontal-default-color: #fff;
@horizontal-inverse-color: #000;

.horizontal-variant(@variant) {
  @color: "horizontal-@{variant}-color";
  color: @@color;
}

.horizontal-default{
  .horizontal-variant(default);
}
.horizontal-inverse{
  .horizontal-variant(inverse);
}


来源:https://stackoverflow.com/questions/23823833/dynamic-variable-names-in-less-css

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