mixin類似于函數(shù),在mixin中定義的變量將作為它的返回值。
下面的例子說(shuō)明了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>
接下來(lái),創(chuàng)建文件 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
接下來(lái)執(zhí)行上面的命令,它將用下面的代碼自動(dòng)創(chuàng)建 style.css 文件:
.myclass { padding-left: 100px; }
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
在 less_Mixin_as_function_return_values.html 文件中保存上面的html代碼。
在瀏覽器中打開此HTML文件,將顯示如下輸出。
更多建議: