我可以使用.LESS变量来缩短这种语法吗?

Eri*_*pel 1 less

我的.less文件根据页面类设置各种元素的颜色.所以对于我的3页(关于,能量,报告),我重复这些元素,我觉得我应该只能以某种方式解决,但我无法弄明白:

@color_about: #54B948;
@color_energy: #C41230;
@color_reports: #FBB040;

.about {
  @color: @color_about;
  h1, .thick-bottom-border, &.thick-bottom-border { color: @color;     }
  &.button:hover, &.button:focus, &.label { background-color: @color; }
}

.energy {
  @color: @color_energy;
  h1, .thick-bottom-border, &.thick-bottom-border { color: @color;         }
  &.button:hover, &.button:focus, &.label { background-color: @color; }
}

.reports {
  @color: @color_reports;
  h1, .thick-bottom-border, &.thick-bottom-border { color: @color;     }
  &.button:hover, &.button:focus, &.label { background-color: @color; }
}
Run Code Online (Sandbox Code Playgroud)

Luk*_*age 5

是的,您可以使用(〜"")输出变量作为选择器.

.do_color("about", #54B948);
.do_color("energy", #C41230);
.do_color("reports", #FBB040);

.do_color(@name, @color) {
  (~".@{name}") {
    h1, .thick-bottom-border, &.thick-bottom-border { color: @color;     }
    &.button:hover, &.button:focus, &.label { background-color: @color; }
  }
}
Run Code Online (Sandbox Code Playgroud)