我理解LESS没有if / else结构,而是依赖于看守的mixin语句。但它似乎无法在mixin中设置LESS CSS变量。
任何人都知道如何实现某些效果。
if( ispercentage( @height ) {
@height: "1024px";
}
// Calculations dependent on @height
@textAreaHeight = 0.5 * @height;
@buttonHeight = 0.2 * @height;
我查看了其他几个stackoverflow问题,包括: LESS CSS - Setting variable within mixin
答案 0 :(得分:2)
是的,可以做到。有一个错误需要解决。
示例LESS代码
//Set up guarded mixins
.setHeight(@h) when (ispercentage(@h)) {
@height: 1024px;
}
.setHeight(@h) when not (ispercentage(@h)) {
@height: @h;
}
//set up main height
@mainHeight: 50%;
body { height: @mainHeight;}
//call it by itself to make global
//.setHeight(@mainHeight); <-this failed (appears to be a bug)
.setHeight(50%); // <-this worked
.subsection { height: @height; /* just to show it is setting it */}
//use it for other globals
@textAreaHeight: 0.5 * @height;
@buttonHeight: 0.2 * @height;
textarea { height: @textAreaHeight}
button { height: @buttonHeight}
//override it locally
body.fixedHeight {
.setHeight(300px);
@textAreaHeight: 0.333 * @height;
height: @height;
textarea { height: @textAreaHeight}
}
CSS输出示例
body {
height: 50%;
}
.subsection {
height: 1024px; /* just to show it is setting it */
}
textarea {
height: 512px;
}
button {
height: 204.8px;
}
body.fixedHeight {
height: 300px;
}
body.fixedHeight textarea {
height: 99.9px;
}
答案 1 :(得分:1)
您可以创建一个被类型“保护”的mixin,例如
.doHeightSet(@height) when ispercentage(@height) {
.doheightSet(1024px)
}
.doHeightSet(@height) when not ispercentage(@height) {
// Calculations dependent on @height
@textAreaHeight: 0.5 * @height;
@buttonHeight: 0.2 * @height;
/* use variables here */
}
抱歉,我没有时间试试这个,所以我可能犯了一个语法错误。
修改更正的语法。