使用小数生成类名

问题描述 投票:0回答:1

我想创建一个类.rating-1-1 {},.rating-1-2 {},直到10.0。这里1-1表示它是1.1。我想用 - 替换小数点。在我当前的实现中,它生成为.rating-1.1,.rating-1.2这个类不适用于应用css样式。

试试代码

@iterations: 10;
.rate-loop (@i) when (@i > 0) {
    .rate-@{i} {
        width: percentage((@i / @iterations));
    }
    .rate-loop(@i - 0.1);
}
.rate-loop (@iterations);
css less
1个回答
1
投票

使用简单的算术和numeric functions,例如:

.loop(3);
.loop (@n, @j: @n) when (@j > 0) {
    .loop(@j, @j - 1/10);
    @i: floor(@j);            // integer part
    @f: floor(mod(@j, 1)*10); // fractional part
    .foo-@{i}-@{f} {
        width: 10% * @j;
    }
}

这将生成从foo-0-0foo-3-0的类。

Demo


如果数字转换不适用,请使用replace

© www.soinside.com 2019 - 2024. All rights reserved.