如何在LESS中的变量中使用变量名?

时间:2023-02-05 19:35:55

I have this code snippet:

我有这段代码:

// Color settings 
.setCat(@x) {
    .cat@{x} {
        .menu-link {
            &.selected, &:hover { background: ~@{"catColor@{x}"}; }
            &:hover:after { border-top-color: ~@{"catColor@{x}"}; }
            &:only-child:after { border-top-color: transparent; }
        }
        .menu-link-submenu { background: ~@{"catColor@{x}"}; }
    }
}

.setCat(1);

I hope you can see what I am trying to do. I want the @catColor1 output which LESS then will compile into my hex color stored in that variable.

我希望你能看到我想要做的事情。我想要@catColor1输出,然后LESS将编译成存储在该变量中的十六进制颜色。

Is this possible? Is there a better way?

这可能吗?有没有更好的办法?

1 个解决方案

#1


3  

You need to nest both calls into the string. I've made the processing all go through a single new variable useColor (which you do not have to do, I just think it looks cleaner):

您需要将两个调用嵌套到字符串中。我已经完成了处理,只需要一个新的变量useColor(你不必这样做,我觉得它看起来更干净):

.setCat(@x) {
    .cat@{x} {
        @useColor: ~"@{catColor@{x}}";
        .menu-link {
            &.selected, &:hover { background: @useColor; }
            &:hover:after { border-top-color: @useColor; }
            &:only-child:after { border-top-color: transparent; }
        }
        .menu-link-submenu { background: @useColor; }
    }
}

So assuming this LESS:

所以假设这个少:

@catColor1: #fff;
@catColor2: #aaa;

.setCat(1);
.setCat(2);

Produces this CSS:

生成这个CSS:

.cat1 .menu-link.selected,
.cat1 .menu-link:hover {
  background: #ffffff;
}
.cat1 .menu-link:hover:after {
  border-top-color: #ffffff;
}
.cat1 .menu-link:only-child:after {
  border-top-color: transparent;
}
.cat1 .menu-link-submenu {
  background: #ffffff;
}
.cat2 .menu-link.selected,
.cat2 .menu-link:hover {
  background: #aaaaaa;
}
.cat2 .menu-link:hover:after {
  border-top-color: #aaaaaa;
}
.cat2 .menu-link:only-child:after {
  border-top-color: transparent;
}
.cat2 .menu-link-submenu {
  background: #aaaaaa;
}

#1


3  

You need to nest both calls into the string. I've made the processing all go through a single new variable useColor (which you do not have to do, I just think it looks cleaner):

您需要将两个调用嵌套到字符串中。我已经完成了处理,只需要一个新的变量useColor(你不必这样做,我觉得它看起来更干净):

.setCat(@x) {
    .cat@{x} {
        @useColor: ~"@{catColor@{x}}";
        .menu-link {
            &.selected, &:hover { background: @useColor; }
            &:hover:after { border-top-color: @useColor; }
            &:only-child:after { border-top-color: transparent; }
        }
        .menu-link-submenu { background: @useColor; }
    }
}

So assuming this LESS:

所以假设这个少:

@catColor1: #fff;
@catColor2: #aaa;

.setCat(1);
.setCat(2);

Produces this CSS:

生成这个CSS:

.cat1 .menu-link.selected,
.cat1 .menu-link:hover {
  background: #ffffff;
}
.cat1 .menu-link:hover:after {
  border-top-color: #ffffff;
}
.cat1 .menu-link:only-child:after {
  border-top-color: transparent;
}
.cat1 .menu-link-submenu {
  background: #ffffff;
}
.cat2 .menu-link.selected,
.cat2 .menu-link:hover {
  background: #aaaaaa;
}
.cat2 .menu-link:hover:after {
  border-top-color: #aaaaaa;
}
.cat2 .menu-link:only-child:after {
  border-top-color: transparent;
}
.cat2 .menu-link-submenu {
  background: #aaaaaa;
}