LESS - Mixin & 返回值



描述

Mixin 類似於函式,在 Mixin 中定義的變數將作為其返回值。

示例

以下示例演示了在 LESS 檔案中使用Mixin & 返回值

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Mixins & return values</title>
   </head>

   <body>
      <div class = "myclass">
         <h2>Welcome to Tutorialspoint</h2>
         <p>LESS is a CSS pre-processor that enables customizable, 
         manageable and reusable style sheet for web site.</p>
      </div>
   </body>
</html>

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

style.less

.padding(@x, @y) {
   @padding: ((@x + @y) / 2);
}

.myclass{
   .padding(80px, 120px);  // call to the mixin
   padding-left: @padding; //  returns the value
}

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

lessc style.less style.css

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

style.css

.myclass {
   padding-left: 100px;
}

輸出

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

  • 將上述 html 程式碼儲存在less_Mixin_as_function_return_values.html檔案中。

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

LESS Mixin & return values
less_mixins_as_functions.htm
廣告

© . All rights reserved.