目录

条件混合(Conditional Mixins)

描述 (Description)

您可以使用default函数将mixin与其他混合匹配相匹配,并创建看起来像elsedefault语句的conditional mixins

例子 (Example)

以下示例演示了在LESS文件中使用条件mixins -

<!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>

接下来,创建style.less文件。

style.less

.mixin (@a) when (@a > 22px) {
   color:blue;
}
.mixin (@a) when (@a <= 20px) {
   color:red;
}
.mixin (@a) {
   font-size: @a;
}
.myclass { .mixin(20px) }

您可以使用以下命令将style.less编译为style.css -

lessc style.less style.css

执行上面的命令,它将使用以下代码自动创建style.css文件 -

style.css

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

输出 (Output)

请按照以下步骤查看上述代码的工作原理 -

  • 将上述html代码保存在conditional_mixins.html文件中。

  • 在浏览器中打开此HTML文件,将显示以下输出。

Mixin Guards
↑回到顶部↑
WIKI教程 @2018