【发布时间】:2015-07-13 12:11:53
【问题描述】:
我的问题是:如何在 Less 中构建字符串,这取决于可变数量的参数。例如,我想做一个 mixin,它可以帮助我编写 @font-face CSS 规则。所以我需要为我的字体的任意数量的格式(.eot、.ttf、.oft、.woff、.woff2、.svg)构建src:... fonts 属性。这是我处理列表中所有字体格式的 Less 循环:
// @some-types - it is my font formats list, just smth. like 'otf', 'eot',...
// @old-src-value - it is string with src for my font from previous loop
// @counter - it is my loop counter
.make-font-src(@some-types; @old-src-value; @counter) when (@counter <= length(@some-types)) {
// Here we get next font format from @some-types
@font-type: extract(@some-types, @counter);
// Used for building 'format("opentype")' - like part of src value string
.get-font-format(@font-type);
// Building a part of src value string for this iteration
@src-value: e('@{old-src-value}, url("@{font-path}@{font-filename}.@{font-type}") format("@{font-format}")');
// Recursive call of this mixin for looping
.make-font-src(@some-types; @src-value; (@counter + 1));
}
所以我被困在如何获取完整的 src 值字符串上,什么时候所有字体格式都将在循环中处理?另请参考this codepen demo。
【问题讨论】:
-
抱歉之前的评论有误,这不会导致递归定义错误,因为您使用了不同的变量。