Less-mixin-guarded-namespaces

提供:Dev Guides
移動先:案内検索

LESS-保護された名前空間

説明

ガードがネームスペースに適用されると、ガード条件がtrueを返す場合にのみ、ネームスペースによって定義されたミックスインが使用されます。 *名前空間ガード*はミックスインのガードに似ています。

次の例は、LESSファイルでの_guarded namespaces_の使用を示しています-

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css"/>
      <title>Guarded Namespaces</title>
   </head>

   <body>
      <h2>Welcome to finddevguides</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.finddevguides.com/less/lib.less";
#namespace when (@color = blue) {
   .mixin() {
      color: red;
   }
}

p {
   #namespace .mixin();
}

次のコードは、パスリンクから_lib.less_ファイルを_style.less_にインポートします。/less/lib.less[https://www.finddevguides.com/less/lib.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_namespacesl ファイルに保存します。
  • このHTMLファイルをブラウザで開くと、次の出力が表示されます。

LESS Guarded Namespacess