LESS 保护的命名空间

2018-01-09 19:45 更新

描述

当guard应用于命名空间时,只有在guard条件返回true时才使用由命名空间定义的mixin。 命名空间防护类似于mixins上的guard。


例子

下面的示例演示了在LESS文件中使用防护名称空间:

<html>
<head>
  <link rel="stylesheet" href="style.css" type="text/css" />
  <title>Guarded Namespaces</title>
</head>
<body>
<h2>Welcome to Tutorialspoint</h2>
<p>This will paragraph be displayed red, when (@color = blue) in style.less and when color is other than blue, then this paragraph will be default black.</p>
</body>
</html>


接下来,创建文件 style.less 。

style.less

@import "//www.w3cschool.cn/less/lib.less";
#namespace when (@color = blue) {
  .mixin() {
   color: red;
  }
}
p{
 #namespace .mixin();
}

将路径从http://www.w3cshool.cn/statics/demosource/lib.less导入到style.less中的lib.less文件


lib.less

@color: blue;


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

lessc style.less style.css


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

style.css

p {
  color: red;
}


输出

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

  • less_mixin_guarded_namespaces.html 文件中保存html代码。

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


shuc

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号