目录

Mixin和返回值(Mixin and return values)

描述 (Description)

Mixins与函数类似,mixin中定义的变量将作为其返回值。

例子 (Example)

以下示例演示了在LESS文件中使用mixin & return values -

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Mixins & return values</title>
   </head>
   <body>
      <div class = "myclass">
         <h2>Welcome to IoWiki</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

.padding(@x, @y) {
   @padding: ((@x + @y)/2);
}
.myclass{
   .padding(80px, 120px);  // call to the mixin
   padding-left: @padding; //  returns the value
}

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

lessc style.less style.css

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

style.css

.myclass {
   padding-left: 100px;
}

输出 (Output)

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

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

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

混合和返回值
↑回到顶部↑
WIKI教程 @2018