您可以使用默認函數(shù)將mixin與其他混合匹配進行匹配,并創(chuàng)建 else 或默認 語句。
下面的例子演示了在LESS文件中使用條件混合:
<!doctype html> <head> <title>Conditional Mixins</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <h2>Example of Conditional Mixins</h2> <p class="myclass">LESS enables customizable, manageable and reusable style sheet for web site.</p> </body> </html>
接下來,創(chuàng)建文件 style.less 。
.mixin (@a) when (@a > 22px){ color:blue; } .mixin (@a) when (@a <= 20px){ color:red; } .mixin (@a) { font-size: @a; } .myclass { .mixin(20px) }
lessc style.less style.css
.myclass { color: red; font-size: 20px; }
將上面的html代碼保存在conditional_mixins.html文件中。
在瀏覽器中打開此HTML文件,將顯示如下輸出。
更多建議: