1、内置函数
Less 内置了多种函数用于转换颜色、处理字符串、算术运算等。这些函数在Less 函数手册中有详细介绍。
函数的用法非常简单。下面这个例子将介绍如何利用 percentage 函数将 0.5 转换为 50%,将颜色饱和度增加 5%,以及颜色亮度降低 25% 并且色相值增加 8 等用法:
@base: #f04615;@width: 0.5;.class {width: percentage(@width); // returns `50%`color: saturate(@base, 5%);background-color: spin(lighten(@base, 25%), 8);}
2、自定义函数
//无参函数.box() {width: 100px;height: 200px;background-color: aqua;}//带参函数.boxWithParam(@w, @h, @c) {width: @w;height: @h;background-color: @c;}//带默认参数函数.boxWithDefaultParam(@w: 100px, @h: 200px, @c: red) {width: @w;height: @h;background-color: @c;}div {//.box();//.boxWithParam(60px,100px,yellow);//.boxWithDefaultParam();//.boxWithDefaultParam(60px,100px,yellow);//可指定要传值的参数.boxWithDefaultParam(@h: 600px, @c: yellow);}
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>less</title><!-- 这里引入的是lessc styles.less styles.css编译后的css文件 --><link rel="stylesheet" type="text/css" href="./styles.css" /></head><body><div><span> aaa </span></div></body></html>
