LESS - 邏輯運算子守衛



描述

您可以使用 and 關鍵字來處理帶有守衛的邏輯運算子。您可以使用 and 關鍵字組合守衛條件,並使用 not 關鍵字否定條件。

示例

以下示例演示了在 LESS 檔案中使用守衛邏輯運算子 -

<!doctype html>
   <head>
      <title>Guard Logical Operators</title>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <h2>Example of Guard Logical Operators</h2>
      <p class = "class1">Hello World...</p>
      <p class = "class2">Welcome to Tutorialspoint...</p>
   </body>
</html>

接下來,建立 style.less 檔案。

style.less

.mixin (@a) when (@a > 50%) and (@a > 5px) {
   font-size: 14px;
}

.mixin (@a) when not (@a < 50%) and not (@a < 5px) {
   font-size: 20px;
}
.mixin (@a) {
   color: @a;
}

.class1 { .mixin(#FF0000) }
.class2 { .mixin(#555) }

您可以使用以下命令將 style.less 編譯為 style.css -

lessc style.less style.css

執行上述命令;它將自動建立 style.css 檔案,其中包含以下程式碼 -

style.css

.class1 {
   font-size: 20px;
   color: #FF0000;
}

.class2 {
   font-size: 20px;
   color: #555;
}

輸出

請按照以下步驟檢視上述程式碼的工作原理 -

  • 將以上 html 程式碼儲存在 guard_logical_operators.html 檔案中。

  • 在瀏覽器中開啟此 HTML 檔案,將顯示以下輸出。

Mixin Guards
less_mixin_guards.htm
廣告

© . All rights reserved.