LESS - 型別檢查函式



描述

您可以使用內建的型別檢查函式來確定匹配 mixin 的值型別。為此,您可以使用is 函式。以下是可用函式的列表:

  • iscolor (是否為顏色)
  • isnumber (是否為數字)
  • isstring (是否為字串)
  • iskeyword (是否為關鍵字)
  • isurl (是否為URL)

上面列出的函式屬於基本型別檢查。您可以使用以下函式檢查值是否具有特定單位:

  • ispixel (是否為畫素)
  • ispercentage (是否為百分比)
  • isem (是否為em)
  • isunit (是否具有單位)

示例

以下示例演示了在 LESS 檔案中使用型別檢查函式:

<!doctype html>
   <head>
      <title>Type Checking Functions</title>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>

   <body>
      <h2>Example of Type Checking Functions</h2>
      <p class = "myclass">Hello World!!!Welcome to Tutorialspoint...</p>
   </body>
</html>

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

style.less

.mixin (@a; @b: red) when (iscolor(@b)){
   color:blue;
}

.mixin (@a) {
   font-size: @a;
}

.myclass { .mixin(20px) }

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

lessc style.less style.css

現在執行上述命令;它將自動建立包含以下程式碼的style.css檔案:

style.css

.myclass {
   color: blue;
   font-size: 20px;
}

輸出

按照以下步驟檢視上述程式碼的工作方式:

  • 將上述 html 程式碼儲存到type_checking_functions.html檔案中。

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

Mixin Guards
less_mixin_guards.htm
廣告
© . All rights reserved.