Wil*_*ney 8 html css css3 less
我正在尝试调整几个兄弟div的位置,根据他们的兄弟姐妹.目前,他们都是position: absolute,但这不是一成不变的.
它们每个都有几百个像素高,但我希望它们相互重叠,以便后面的那些只能"偷看"在前面几个像素之上.我能想到的最简单的方法是使用Less mixin nth-of-type,而不是仅将规则应用于匹配的一个索引,而是将索引传递给mixin.基本上,我想要这个:
&:nth-of-type(@n) {
top: @n * 20px;
}
Run Code Online (Sandbox Code Playgroud)
编辑:我目前在做什么:
&:nth-of-type(1) {
.card_offset(1);
}
&:nth-of-type(2) {
.card_offset(2);
}
&:nth-of-type(3) {
.card_offset(3);
}
&:nth-of-type(4) {
.card_offset(4);
}
Run Code Online (Sandbox Code Playgroud)
显然这不是最理想的.在Less中有更好的方法吗?
或者,是否有一个类似于某种东西的CSS字段'layout-height'会在布局中给div一定的高度(而不是它的全高)?
Sco*_*ttS 10
假设您事先知道了元素的数量(或者在某种程度上重新计算了你的css),那么如果放入我最初在堆栈溢出时发现的循环结构,基本上你的工作.
少量代码
.loop(@index) when (@index > 0) {
//set top amount
&:nth-of-type(@{index}) { //NOTE: 1.3.3 and under is just @index, not @{index}
top: @index * 20px;
}
// next iteration
.loop(@index - 1);
}
// end the loop when index is 0
.loop(0) {}
.yourElem {
@n: 6; //num of elements (could skip this and just put number in)
.loop(@n);
}
Run Code Online (Sandbox Code Playgroud)
输出
.yourElem:nth-of-type(6) {
top: 120px;
}
.yourElem:nth-of-type(5) {
top: 100px;
}
.yourElem:nth-of-type(4) {
top: 80px;
}
.yourElem:nth-of-type(3) {
top: 60px;
}
.yourElem:nth-of-type(2) {
top: 40px;
}
.yourElem:nth-of-type(1) {
top: 20px;
}
Run Code Online (Sandbox Code Playgroud)