Less 函数中文详解

逻辑函数


if(condition,value1,value2)

根据布尔值返回两个值中的一个

参数:

  • condition: 布尔值

  • value1: 布尔值为true时的返回值.

  • value2: 布尔值为false时的返回值.

@some: foo;

div {
    margin: if((2 > 1), 0, 3px);
    color:  if((iscolor(@some)), @some, black);
}

输出:

div {
    margin: 0;
    color:  black;
}

不同类型的condition

if(not (true), foo, bar);
if((true) and (2 > 1), foo, bar);
if((false) or (isstring("boo!")), foo, bar);

boolean(condition)

计算真或假

参数:

  • condition: 布尔表达式

返回值:truefalse

@bg: black;
@bg-light:boolean(luma(@bg) > 50%);

div {
  background: @bg; 
  color: if(@bg-light, black, white);
}

输出:

div {
  background: black;
  color: white;
}

字符串函数


color(string)

解析颜色,将代表颜色的字符串转换为颜色值.

参数:

  • string: 代表颜色值的字符串

返回值:color - 颜色对象

 color("#aaa"); // #aaa

convert(number,units)

将数字从一种单位转换到另一种单位。

第一个参数为带单位的数值,第二个参数为单位。如果两个参数的单位是兼容的,则数字的单位被转换。如果两个参数的单位不兼容,则原样返回第一个参数。

兼容的单位包括:

长度: m, cm, mm, in, pt, pc

时间: s, ms

角度: rad, deg, grad, turn


参数:

  • number: 带单位的浮点数。

  • units: 标识符, 字符串或转义后的值

返回值: number

convert(9s,"ms")   //9000ms
convert(14cm, mm)  //140mm
convert(8, mm)     //8

data-uri(mimetype,url)

将资源内联进样式表,如果开启了 ieCompat 选项并且资源太大,或者此函数的运行环境为浏览器,则会回退到直接使用 url() 。如果没有指定 MIME,则 node 将使用 mime 包来决定正确的 mime 类型。

参数:

  • mimetype: (可选) MIME 字符串。

  • url: 需要内嵌的文件的 URL 。

data-uri('../data/image.jpg');
//输出: url('data:image/jpeg;base64,bm90IGFjdHVhbGx5IGEganBlZyBmaWxlCg==');
//浏览器端输出: url('../data/image.jpg');

data-uri('image/jpeg;base64', '../data/image.jpg');
//url('data:image/jpeg;base64,bm90IGFjdHVhbGx5IGEganBlZyBmaWxlCg==');

unit(dimension,unit)

删除或更换、添加单位。

参数

  • dimension: 带单位或不带单位的数字。

  • unit: (可选) 目标单位,如果省略此参数,则删除单位。

//添加单位
unit(5, px) //输出: 5px

//删除单位
unit(5em) //输出: 5

escape(string)

对字符串中的特殊字符做 URL-encoding 编码

这些字符不会被编码:,, /, ?, @, &, +, ', ~, ! , $

被编码的字符是:\<space\>, #, ^, (, ), {, }, |, :, >, <, ;, ], [ , =


参数:

  • string: 需要转义的字符串。

返回值:转义后不带引号的 string 字符串。

escape('a=1')  //a%3D1

注意:如果参数不是字符串的话,函数行为是不可预知的。目前传入颜色值的话会返回 undefined ,其它的值会原样返回。写代码时不应该依赖这个特性,而且这个特性在未来有可能改变。


e(string)

用于对 CSS 的转义,已经由 ~"value" 语法代替。

它接受一个字符串作为参数,并原样返回内容,不含引号。它可用于输出一些不合法的 CSS 语法,或者是使用 LESS 不能识别的属性。

参数:

  • string - 需要转义的字符串。

返回值: string - 转义后的字符串,不含引号。

filter:e("ms:alwaysHasItsOwnSyntax.For.Stuff()"); 
//filter:ms:alwaysHasItsOwnSyntax.For.Stuff();

注意:也接受经 ~"" 转义的值或者是数字作为参数。任何其它的值将产生错误。


%(string, arguments)

此函数用于格式化字符串。

第一个参数是一个包含占位符的字符串。占位符以百分号 % 开头,后面跟着字母 s、S、d、D、aA。后续的参数用于替换这些占位符。如果你需要输出百分号,可以多用一个百分号来转义 %%。

使用大写的占位符可以将特殊字符按照 UTF-8 编码进行转义。 此函数将会对所有的特殊字符进行转义,除了 ()'~! 。空格会被转义为 %20 。小写的占位符将原样输出特殊字符,不进行转义。

占位符说明:

  • d, D, a, A - 以被任意类型的参数替换 (颜色、数字、转义后的字符串、表达式等)。如果将它们和字符串一起使用,则整个字符串都会被使用,包括引号。然而,引号将会按原样放在字符串中,不会用 "/" 或类似的方式转义。

  • s, S - 可以被除了颜色的之外的任何类型参数替换。如果你将它们和字符串一起使用,则只有字符串的值会被使用,引号会被忽略。

参数:

  • string: 有占位符的格式化字符串。

  • anything* : 用于替换占位符的值。

返回值:string

format-a-d: %("repetitions: %a file: %d", 1 + 2, "directory/file.less");
//format-a-d: "repetitions: 3 file: "directory/file.less"";

format-a-d-upper: %('repetitions: %A file: %D', 1 + 2, "directory/file.less");
//format-a-d-upper: "repetitions: 3 file: %22directory%2Ffile.less%22";

format-s: %("repetitions: %s file: %s", 1 + 2, "directory/file.less");
//format-s: "repetitions: 3 file: directory/file.less";

format-s-upper: %('repetitions: %S file: %S', 1 + 2, "directory/file.less");
//format-s-upper: "repetitions: 3 file: directory%2Ffile.less";

replace(string,pattern,replacement,flags)

用一个字符串替换一段文本。

参数:

  • string: 用于搜索、替换操作的字符串。

  • pattern: 用于搜索匹配的字符串或正则表达式。

  • replacement: 用于替换匹配项的字符串。

  • flags: (可选) 正则表达式参数。

返回值: 被替换之后的字符串。

replace("Hello, Mars?", "Mars\?", "Earth!");  //"Hello, Earth!";
replace("One + one = 4", "one", "2", "gi");  //"2 + 2 = 4";
replace('This is a string.', "(string)\.$", "new $1.");  //'This is a new string.';
replace(~"bar-1", '1', '2');  //bar-2;

列表函数


length(list)

返回列表中元素的个数。

参数:

  • list - 由逗号或空格分隔的元素列表。

返回值:一个代表元素个数的数字。

@list:"banana","tomato","potato","peach";
n:length(@list);             // n: 4;

length(1px solid #0080ff);  // 3

extract(list,index)

返回列表中指定位置的元素。

参数:

  • list - 逗号或空格分隔的元素列表。

  • index - 指定列表中元素位置的数字。

返回值:列表中指定位置的元素。

@list: apple, pear, coconut, orange;
value:extract(@list,3);       // value: coconut;

extract(8px dotted red, 2);  // dotted

数学函数


ceil(number)

向上取整。

参数:

  • number - 浮点数。

返回值: 整数(integer

ceil(2.4)  // 3

floor(number)

向下取整。

参数:

  • number - 浮点数

返回值: 整数(integer

floor(2.6)  // 2

percentage(number)

将浮点数转换为百分比字符串。

参数:

  • number - 浮点数。

返回值: 字符串(string

percentage(0.5)  // 50%

round(number,decimalPlaces)

四舍五入取整。

参数:

  • number: 浮点数

  • decimalPlaces: 可选:四舍五入取整的小数点位置。默认值为0。

返回值: 数字(number

round(1.67) // 2
round(1.67, 1)  // 1.7

sqrt(number)

计算一个数的平方根,并原样保持单位。

参数:

  • number - 浮点数

返回值: 数字(number

sqrt(25cm)   // 5cm
sqrt(18.6%)  // 4.312771730569565%;

abs(number)

计算数字的绝对值,并原样保持单位。

参数:

  • number - 浮点数。

返回值: 数字(number

abs(25cm)    // 25cm
abs(-18.6%)  // 18.6%;

sin(number)

正弦函数。

处理时会将没有单位的数字认为是弧度值。

参数:

  • number - 浮点数。

返回值: 数字(number

sin(1);     // 0.8414709848078965; sine of 1 radian
sin(1deg);  // 0.01745240643728351; sine of 1 degree
sin(1grad); // 0.015707317311820675; sine of 1 gradian

asin(number)

反正弦函数。返回以弧度为单位的角度,区间在 -PI/2 到 PI/2之间。

返回以弧度为单位的角度,区间在 -π/2 和 π/2 之间。

参数:

  • number - 取值范围为 [-1, 1] 之间的浮点数。

返回值: 数字(number

asin(-0.8414709848078965)  // -1rad
asin(0)                    // 0rad
asin(2)                    // NaNrad

cos(number)

余弦函数。

处理时会将没有单位的数字认为是弧度值。

参数:

  • number - 浮点数。

返回值: 数字(number

cos(1)       // 0.5403023058681398 cosine of 1 radian
cos(1deg)    // 0.9998476951563913 cosine of 1 degree
cos(1grad)   // 0.9998766324816606 cosine of 1 gradian

acos(number)

反余弦函数。,区间在 0 到 PI之间。

返回以弧度为单位的角度,区间在 0π 之间。

参数:

  • number - 取值范围为 [-1, 1] 之间的浮点数。

返回值: 数字(number

acos(0.5403023058681398)  // 1rad
acos(1)                   // 0rad
acos(2)                   // NaNrad

tan(number)

正切函数。

处理时会将没有单位的数字认为是弧度值。

参数:

  • number - 浮点数。

返回值: 数字(number

tan(1)// tangent of 1 radian 1.5574077246549023
tan(1deg)// tangent of 1 degree 0.017455064928217585
tan(1grad)// tangent of 1 gradian 0.015709255323664916

atan(number)

反正切函数。

返回以弧度为单位的角度,区间在 -π/2 到 π/2 之间。

参数:

  • number - 浮点数。

返回值: 数字(number

atan(-1.5574077246549023) // -1rad
atan(0)                   // 0rad

pi()

返回圆周率 π (pi);

参数: none

返回值: number

pi()  // 3.141592653589793

pow(number,number)

设第一个参数为A,第二个参数为B,返回A的B次方。

返回值与第一个参数有相同的单位,第二个参数的单位被忽略。

参数:

  • number: base -浮点数。

  • number: exponent - 浮点数。

返回值: 数字(number

pow(0cm,0px) // 1cm

mod(number,number)

返回第一个参数对第二参数取余的结果。

返回值与第一个参数单位相同,第二个参数单位被忽略。这个函数也可以处理负数和浮点数。

参数:

  • number: 浮点数。

  • number: 浮点数。

返回值: 数字(number

mod(0cm, 0px);
mod(11cm, 6px);
mod(-26%,-5);

//NaNcm;
//5cm;
//-1%;

min(value1, ..., valueN)

返回一系列值中最小的那个。

参数: value1, ..., valueN - 一个或多个参与比较的值。

返回值: 最小值。

min(5, 10);
// 5

min(3px, 42px, 1px, 16px);
// 1px

max(value1, ..., valueN)

返回一系列值中最大的那个。

参数: value1, ..., valueN - 一个或多个参与比较的值。

返回值: 最大值。

max(5, 10);
// 10

max(3%, 42%, 1%, 16%);
// 42%

类型函数


isnumber(value)

如果待验证的值为数字则返回 true ,否则返回 false 。

参数:

  • value - 待验证的值或变量。

返回值:如果待验证的值为数字则返回 true ,否则返回 false

isnumber(#ff0);     //false
isnumber(blue);     //false
isnumber("string"); //false
isnumber(1234);     //true
isnumber(56px);     //true
isnumber(7.8%);     //true
isnumber(keyword);  //false
isnumber(url(...)); //false

isstring(value)

如果待验证的值是字符串则返回 true ,否则返回 false 。

参数:

  • value - 待验证的值或变量。

返回值:如果是字符串则返回 true ,否则返回 false

isstring(#ff0);     // false
isstring(blue);//false
isstring("string");//true
isstring(1234);//false
isstring(56px);//false
isstring(7.8%);     // false
isstring(keyword);//false
isstring(url(...));//false

iscolor(value)

如果待验证的值为颜色则返回 true ,否则返回 false 。

参数:

  • value - 待验证的值或变量。

返回值:如果待验证的值为颜色则返回 true ,否则返回 false

iscolor(#ff0);     // true
iscolor(blue);//true
iscolor("string");//false
iscolor(1234);//false
iscolor(56px);//false
iscolor(7.8%);     // false
iscolor(keyword);//false
iscolor(url(...));//false

iskeyword(value)

如果待验证的值为关键字则返回 true ,否则返回 false 。

参数:

  • value - 待验证的值或变量。

返回值:如果待验证的值为关键字则返回 true ,否则返回 false

iskeyword(#ff0);     // false
iskeyword(blue);//false
iskeyword("string");//false
iskeyword(1234);//false
iskeyword(56px);//false
iskeyword(7.8%);     // false
iskeyword(keyword);//true
iskeyword(url(...));//false

isurl(value)

如果待验证的值为 url 则返回 true ,否则返回 false 。

参数:

  • value - 待验证的值或变量。

返回值:如果待验证的值为 url 则返回 true ,否则返回 false

isurl(#ff0);     // false
isurl(blue);//false
isurl("string");//false
isurl(1234);//false
isurl(56px);//false
isurl(7.8%);     // false
isurl(keyword);//false
isurl(url(...));//true

ispixel(value)

如果待验证的值为像素数则返回 true ,否则返回 false 。

参数:

  • value - 待验证的值或变量。

返回值:如果待验证的值为像素数则返回 true ,否则返回 false

ispixel(#ff0);     // false
ispixel(blue);//false
ispixel("string");//false
ispixel(1234);//false
ispixel(56px);//true
ispixel(7.8%);     // false
ispixel(keyword);//false
ispixel(url(...));//false

isem(value)

如果待验证的值的单位是 em 则返回 true ,否则返回 false 。

参数:

  • value - 待验证的值或变量。

返回值:如果待验证的值的单位是 em 则返回 true ,否则返回 false

isem(#ff0);     // false
isem(blue);//false
isem("string");//false
isem(1234);//false
isem(56px);//false
isem(7.8em);//true
isem(keyword);//false
isem(url(...));//false

ispercentage(value)

如果待验证的值为百分比则返回 true ,否则返回 false 。

参数:

  • value - 待验证的值或变量。

返回值:如果待验证的值为百分比则返回 true ,否则返回 false

ispercentage(#ff0);     // false
ispercentage(blue);//false
ispercentage("string");//false
ispercentage(1234);//false
ispercentage(56px);//false
ispercentage(7.8%);     // true
ispercentage(keyword);//false
ispercentage(url(...));//false

isunit(value,unit)

如果待验证的值为指定单位的数字则返回 true ,否则返回 false 。

参数:

  • value - 待验证的值或变量。

  • unit - 单位标示符 (可加引号) 。

返回值:如果待验证的值为指定单位的数字则返回 true ,否则返回 false

isunit(11px, px);//true
isunit(2.2%, px);  // false
isunit(33px, rem);//false
isunit(4rem, rem);//true
isunit(56px,"%");//false
isunit(7.8%,'%');//true
isunit(1234, em);//false
isunit(#ff0, pt);  // false
isunit("mm", mm);//false

颜色定义函数


rgb(red,green,blue)

通过十进制红色、绿色、蓝色三种值 (RGB) 创建不透明的颜色对象。

在 HTML/CSS 中也会用文本颜色值 (literal color values) 定义颜色值,例如 #ff0000。

参数:

  • red: 0-255 的整数或 0-100% 的百分比数

  • green: 0-255 的整数或 0-100% 的百分比数

  • blue: 0-255 的整数或 0-100% 的百分比数

返回值: color

rgb(90, 129, 32)
// #5a8120

rgba(red,green,blue,alpha)

通过十进制红色、绿色、蓝色,以及 alpha 四种值 (RGBA) 创建带alpha透明的颜色对象。

参数:

  • red: 0-255 的整数或 0-100% 的百分比数

  • green: 0-255 的整数或 0-100% 的百分比数

  • blue: 0-255 的整数或 0-100% 的百分比数

  • alpha: 0-1 的整数或 0-100% 的百分比数

返回值: color

rgba(90, 129, 32, 0.5)
// rgba(90, 129, 32, 0.5)

argb(color)

创建格式为 #AARRGGBB 的十六进制颜色值 ( 注意不是#RRGGBBAA!)。

这种格式被用于 IE 、.NET 和 Android 的开发中。

参数:

  • color, 颜色对象。

返回值: string

argb(rgba(90, 23, 148, 0.5));
// #805a1794

hsl(hue,saturation,lightness)

通过色相 (hue),饱和度 (saturation),亮度 (lightness) 三种值 (HSL) 创建不透明的颜色对象。

参数:

  • hue: 0-360 的整数,用于表示度数。

  • saturation: 0-100% 的百分比数或 0-1 的整数。

  • lightness: 0-100% 的百分比数或 0-1 的整数。

返回值: color

hsl(90, 100%, 50%)
// #80ff00

当你想基于一种颜色的通道来创建另一种颜色时很方便,例如: @new: hsl(hue(@old), 45%, 90%);@new 将拥有 @old 的 hue,以及它自身的饱和度与亮度。

hsla(hue,saturation,value,alpha)

通过色相 (hue),饱和度 (saturation),亮度 (lightness),以及 alpha 四种值 (HSLA) 创建透明的颜色对象。

参数:

  • hue: 0-360 的整数,用于表示度数。

  • saturation: 0-100% 的百分比数或 0-1 的整数。

  • lightness: 0-100% 的百分比数或 0-1 的整数。

  • alpha: 0-100% 的百分比数或 0-1 的整数。

返回值: color

hsl(90, 100%, 50%, 0.5)
// rgba(128, 255, 0, 0.5)

hsv(hue,saturation,value)

通过色相 (hue)、饱和度 (saturation)、色调 (value) 三种值 (HSV) 创建不透明的颜色对象。

注意,与 hsl 不同,这是另一种在 Photoshop 中可用的色彩空间。

参数:

  • hue: 0-360 的整数,用于表示度数。

  • saturation: 0-100% 的百分比数或 0-1 的整数。

  • value: 0-100% 的百分比数或 0-1 的整数。

返回值: color

hsv(90, 100%, 50%)
// #408000

hsva(hue,saturation,value,alpha)

通过色相 (hue),饱和度 (saturation),色调 (value),以及 alpha 四种值 (HSVA) 创建透明的颜色对象。

注意,与 hsla 不同,这是另一种在 Photoshop 中可用的色彩空间。

参数:

  • hue: 0-360 的整数,用于表示度数。

  • saturation: 0-100% 的百分比数或 0-1 的整数。

  • value: 0-100% 的百分比数或 0-1 的整数。

  • alpha: 0-100% 的百分比数或 0-1 的整数。

返回值: color

hsva(90, 100%, 50%, 0.5)
//rgba(64, 128, 0, 0.5)

颜色通道函数


hue(color)

从颜色对象的 HSL 颜色空间中提取色色调值。

参数:

  • color - 颜色对象。

返回值: 整数(integer)0-360

hue(hsl(90, 100%, 50%))
// 90

saturation(color)

从颜色对象的 HSL 色彩空间中提取饱和度值。

参数:

  • color - 颜色对象。

返回值: 百分比(percentage)0-100

saturation(hsl(90, 100%, 50%))
// 100%

lightness(color)

从颜色对象的 HSL 色彩空间中提取亮度值。

参数:

  • color - 颜色对象。

返回值: 百分比(percentage)0-100

lightness(hsl(90, 100%, 50%))
// 50%

hsvhue(color)

在颜色对象的 HSV 色彩空间中提取色相值。

参数:

  • color - 颜色对象。

返回值: 整数(integer)0-360

hsvhue(hsv(90, 100%, 50%))
// 90

hsvsaturation(color)

在颜色对象的 HSV 色彩空间提取饱和度值。

参数:

  • color - 颜色对象。

返回值: 百分比(percentage) 0-100

hsvsaturation(hsv(90, 100%, 50%))
// 100%

hsvvalue(color)

在HSV颜色空间中提取颜色对象的值通道。

参数:

  • color - 颜色对象。

返回值: 百分比(percentage) 0-100

hsvvalue(hsv(90, 100%, 50%))
// 50%

red(color)

从颜色对象中提取红色通道值。

参数:

  • color - 颜色对象。

返回值: 整数(integer)0-255

red(rgb(10, 20, 30))
// 10

green(color)

从颜色对象中提取绿色通道值。

参数:

  • color - 颜色对象。

返回值: 整数(integer) 0-255

green(rgb(10, 20, 30))
// 20

blue(color)

从颜色对象中提取蓝色通道值。

参数:

  • color - 颜色对象。

返回值: 整数(integer) 0-255

blue(rgb(10, 20, 30))
// 30

alpha(color)

从颜色对象中提取 alpha 通道值。

参数:

  • color - 颜色对象。

返回值: 浮点数(float)0-1

alpha(rgba(10, 20, 30, 0.5))
// 0.5

luma(color)

计算颜色对象的 luma (perceptual brightness) 值(亮度的百分比表示法)。

使用SMPTEC/Rec。709系数,如WCAG20所建议。在对比度函数中也使用了这种计算。

参数:

  • color - 颜色对象。

返回值: 百分比(percentage) 0-100%

luma(rgb(100, 200, 30))
// 65%

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值