- 语言特性
- Less - 嵌套规则
- Less - 嵌套指令和冒泡
- Less - 操作
- Less - 转义
- Less - 函数
- Less - 命名空间和访问器
- Less - 作用域
- Less - 注释
- Less - 导入
- Less - 变量
- Less - 扩展
- Less - 混合
- Less - 参数化混合
- Less - 混合作为函数
- Less - 将规则集传递给混合
- Less - 导入指令
- Less - 导入选项
- Less - 混合守卫
- Less - CSS 守卫
- Less - 循环
- Less - 合并
- Less - 父选择器
- 函数
- Less - 杂项函数
- Less - 字符串函数
- Less - 列表函数
- Less - 数学函数
- Less - 类型函数
- Less - 颜色定义函数
- Less - 颜色通道函数
- Less - 颜色操作
- Less - 颜色混合函数
- 用法
- Less - 命令行用法
- 在浏览器中使用 Less
- Less - 浏览器支持
- Less - 插件
- Less - 程序化用法
- Less - 在线编译器
- Less - GUI
- Less - 编辑器和插件
- Less - 第三方编译器
- Less - 框架
- Less 有用资源
- Less - 快速指南
- Less - 有用资源
- Less - 讨论
LESS - 模式匹配
描述
您可以通过向混合传递参数来更改其行为。
考虑一个简单的 LESS 代码片段:
.mixin(@a; @color) { ... }
.line {
.mixin(@color-new; #888);
}
您可以使用不同的 @color-new 值来创建不同的混合行为,如下面的代码所示。
.mixin(dark; @color) {
color: darken(@color, 15%);
}
.mixin(light; @color) {
color: lighten(@color, 15%);
}
@color-new: dark;
.line {
.mixin(@color-new; #FF0000);
}
如果将 @color-new 的值设置为 dark,则它将以较深的颜色显示结果,因为混合定义将 dark 作为第一个参数匹配。
示例
以下示例演示了在 LESS 文件中使用模式匹配:
<!doctype html>
<head>
<title>Pattern Matching</title>
<link rel = "stylesheet" href = "style.css" type = "text/css" />
</head>
<body>
<h2>Example of Pattern Matching</h2>
<p class = "myclass">Welcome to Tutorialspoint...</p>
</body>
</html>
接下来,创建 style.less 文件。
style.less
.mixin(dark; @color) {
color: darken(@color, 15%);
}
.mixin(light; @color) {
color: lighten(@color, 15%);
}
@color-new: dark;
.myclass {
.mixin(@color-new; #FF0000);
}
您可以使用以下命令将 style.less 编译为 style.css:
lessc style.less style.css
执行上述命令;它将自动创建 style.css 文件,其中包含以下代码:
style.css
.myclass {
color: #b30000;
}
输出
按照以下步骤查看上述代码的工作原理:
将上述 html 代码保存在 pattern-matching.html 文件中。
在浏览器中打开此 HTML 文件,将显示以下输出。
less_parametric_mixins.htm
广告