当前位置: 首页 > 文档资料 > Less 入门教程 >

保护逻辑运算符(Guard Logical Operators)

优质
小牛编辑
138浏览
2023-12-01

描述 (Description)

您可以使用and关键字来处理带有警卫的逻辑运算符。 您可以使用and关键字组合保护条件, and使用not关键字取消条件。

例子 (Example)

以下示例演示了在LESS文件中使用保护逻辑运算符 -

<!doctype html>
   <head>
      <title>Guard Logical Operators</title>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
   </head>
   <body>
      <h2>Example of Guard Logical Operators</h2>
      <p class = "class1">Hello World...</p>
      <p class = "class2">Welcome to xnip...</p>
   </body>
</html>

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

style.less

.mixin (@a) when (@a > 50%) and (@a > 5px) {
   font-size: 14px;
}
.mixin (@a) when not (@a < 50%) and not (@a < 5px) {
   font-size: 20px;
}
.mixin (@a) {
   color: @a;
}
.class1 { .mixin(#FF0000) }
.class2 { .mixin(#555) }

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

lessc style.less style.css

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

style.css

.class1 {
   font-size: 20px;
   color: #FF0000;
}
.class2 {
   font-size: 20px;
   color: #555;
}

输出 (Output)

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

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

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

Mixin Guards