LESS - Mixin 作用域



描述

如果變數直接在呼叫者的作用域中定義,則無法在呼叫者的作用域中覆蓋它們。但是,變數不受保護,如果在呼叫者的父作用域中定義,則會被覆蓋。

示例

以下示例演示了 LESS 檔案中 *mixin 作用域* 的用法:

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Mixins Scope</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

@val: 20px; // callers parent scope - no protection
.mixin() {
   @val: 10px;
   @definedOnlyInMixin: 10px;
}

.myclass {
   padding-left: @val * @definedOnlyInMixin;
   .mixin();
}

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

lessc style.less style.css

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

style.css

.myclass {
   padding-left: 100px;
}

輸出

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

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

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

LESS Mixin Scope
less_mixins_as_functions
廣告
© . All rights reserved.