LESS - 通过循环两个变量创建ID [英] LESS - Create ID by looping through two variables
本文介绍了LESS - 通过循环两个变量创建ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个循环,输出坐标-2,-2到2,2之间的所有可能的组合。有没有办法做到这一点,而不创建多个循环?
I am trying to create a loop that outputs all of the possible combinations between coordinates -2,-2 to 2,2. Is there any way to do this without creating multiple loops?
#p1x0,#p2x0,#p-1x0,#p-2x0,#p1x1,#p-1x-1,#p-1x1,#p1x-1,#p2x2,#p-2x-2,p2x-2,p-2x2,#p2x1,#p2x-1,#p1x2,#p1x-2,#p-2x1,#p-2x-1,#p-1x2,#p-1x-2,#p0x-1,#p0x-2,#p0x0,#p0x1,#p0x2{}
当前尝试
Current Attempt
#cube-side {
border:red;
}
.create-cubes(@n, @i: -2, @z: -2, @side-sum:@i + @z) when (@side-sum =< @n) {
& when (@i < @z) {
.create-cubes(@n, @i+1);
}
& when (@z < @i) {
.create-cubes(@n, @z+1);
}
#p@{i}x@{z}:extend(#cube-side) {}
}
.create-cubes(4);
输出
b $ b
Output
#cube-side,
#p-2x-2 {
border: red;
}
推荐答案
/ o any loops at all:
There's way to do this w/o any loops at all:
#cube-side {
border: red;
}
-2, -1, 0, 1, 2 {
#p&x&:extend(#cube-side) {}
}
虽然对于一个任意的值列表,一个嵌套循环是最简单的解决方案(参见 example ),例如(pure Less):
Though for an arbitrary list of values just a nested loop is the simplest solution of course (see for example), e.g. (in "pure Less") something like:
.create-cubes(-2, 2);
.create-cubes(@min, @max) {
.i; .i(@i: @min) when (@i <= @max) {
.j; .i(@i + 1);
}
.j(@j: @min) when (@j <= @max) {
#p@{i}x@{j}:extend(#cube-side) {}
.j(@j + 1);
}
}
这篇关于LESS - 通过循环两个变量创建ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文