我想在Less中进行for循环。 在Less中是否可以实现这一点? 我知道它具有评估js的能力,但到这种程度吗?
我想在Less中进行for循环。 在Less中是否可以实现这一点? 我知道它具有评估js的能力,但到这种程度吗?
我建议使用Twitter Bootstrap。他们正在用这种方式构建他们的网格系统。他们在less mixin中使用循环和递归,而不是手动输入每个所需的类。
有趣的部分在less目录下的mixins.less文件中,位于“// The Grid”注释下方(第516行)。有趣的部分是:
#grid {
.core (@gridColumnWidth, @gridGutterWidth) {
.spanX (@index) when (@index > 0) {
(~".span@{index}") { .span(@index); }
.spanX(@index - 1);
}
.spanX (0) {}
...
.span (@columns) {
width: (@gridColumnWidth * @columns) + (@gridGutterWidth * (@columns - 1));
}
...
在 less 目录下的 grid.less 文件中,它是这样被称呼的:
#grid > .core(@gridColumnWidth, @gridGutterWidth);
其中包括但不限于:
.span12 {
width: 940px;
}
.span11 {
width: 860px;
}
.span10 {
width: 780px;
}
...
在 bootstrap.css 的第170行。
要查看 @gridColumnWidth、@gridGutterWidth 和其他变量,请查看 variables.less 文件(第184行)。
请确保已安装最新版本的 lessc node 编译器。
(~".span@{index}")
变成了 span@{index}
http://leafo.net/lessphp/docs/#selector_expressions - yunzen这是一个hack但是... 将此添加到您的less文件中。
@testColor: green;
unusedclass {
unknownprop: ~`loopCss(10,".group{{i}} { background-color: @{testColor}; }")`
}
function loopCss(count,cssTemplate) {
// end the existing unused class
var result = "0;}\n";
// Add your custom css
for(var i=0; i<count; i++) result += cssTemplate.replace("{{i}}",i) +"\n";
// Handle the close bracket from unused class
result += "unusedclass {";
return result;
}
现在在 less.js 之前引入 loopCss.js,它应该可以工作了。
@height: \
document.body.clientHeight`;`。 - T.J. Crowder