codecamp

Less 循环

Loops语句允许我们多次执行一个语句或一组语句。 当递归mixin与 Guard表达式模式匹配组合时,可以创建各种迭代/循环结构。


例子

下面的例子演示了在LESS文件中使用循环:

loop_example.htm

<!doctype html>
<head>
   <link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="cont">
   <h2>Welcome to W3Cschool</h2>
   <p>The largest Tutorials Library on the web. </p>
</div>
</body>
</html>


接下来,创建文件 style.less

风格

.cont(@count) when (@count > 0) {
  .cont((@count - 1));
  width: (25px * @count);
}
div {
  .cont(7);
}


您可以使用以下命令将 style.less 文件编译为 style.css :

lessc style.less style.css


接下来执行上面的命令,它将用下面的代码自动创建 style.css 文件:

style.css

div {
  width: 25px;
  width: 50px;
  width: 75px;
  width: 100px;
  width: 125px;
  width: 150px;
  width: 175px;
}


输出

让我们执行以下步骤,看看上面的代码如何工作:

  • 将上述html代码保存在 loop_example.htm 文件中。

  • 在浏览器中打开此HTML文件,将显示如下输出。


输出

Less CSS Guards
Less 合并
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Less 基本教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }