- 语言特性
- LESS - 嵌套规则
- LESS - 嵌套指令和冒泡
- LESS - 运算
- LESS - 转义
- LESS - 函数
- LESS - 命名空间和访问器
- LESS - 作用域
- LESS - 注释
- LESS - 导入
- LESS - 变量
- LESS - 扩展
- LESS - Mixin
- LESS - 参数化 Mixin
- LESS - Mixin 作为函数
- LESS - 向 Mixin 传递规则集
- LESS - 导入指令
- LESS - 导入选项
- LESS - Mixin 保护
- 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 - Mixin 作用域
描述
包含变量的 Mixin 在调用者的作用域内可见并可以使用。但是有一个例外,如果调用者包含同名的变量,则该变量不会复制到调用者的作用域中。只有调用者作用域内的变量受到保护,继承的变量会被覆盖。
示例
以下示例演示了 LESS 文件中Mixin 作用域 的用法:
<html>
<head>
<link rel = "stylesheet" href = "style.css" type = "text/css" />
<title>Mixins Scope</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>
接下来,创建style.less 文件。
style.less
.mixin() {
@bgcolor: #C0C0C0;
}
.myclass{
.mixin();
background-color: @bgcolor;
}
您可以使用以下命令将style.less 编译为style.css:
lessc style.less style.css
执行上述命令;它将自动创建包含以下代码的style.css 文件:
style.css
.myclass {
background-color: #C0C0C0;
}
输出
按照以下步骤查看上述代码的工作方式:
将上述 html 代码保存到less_mixin_as_function_scope.html 文件中。
在浏览器中打开此 HTML 文件,将显示以下输出。
当变量直接在调用者的作用域中定义时,无法覆盖调用者的作用域中的变量。更多信息,请点击此处。
less_mixins_as_functions.htm
广告