◆
Sass math 数学函数用法大全
sass 中有关 math 的函数操作
math.ceil($number): 向上取整math.clamp($min, $number, $max):如果number小于min返回min,如果大于max则返回max,否则返回其本身。math.floor($number): 向下取整math.max($number...): 返回最大值math.min($number...): 返回最小值math.round($number): 返回最接近该数的一个整数,四舍五入。math.abs($number): 返回一个数值的绝对值。math.pow($base, $exponent): 返回基数(base)的指数(exponent)次幂math.sqrt($number): 返回一个数的平方根math.compatible($number1, $number2): 返回一个布尔值,判断num1与num2是否可以进行比较math.is-unitless($number): 判断number是否有无单位,返回一个boolean值math.unit($number): 返回number的字符串单位math.div($number1, $number2): 返回两个数相除的结果,同等单位会被抵消,两个数的的单位要保持一致,或者num1有单位,num2无单位,反之不可,当num1有单位时,返回结果会带上num1的单位math.percentage($number): 将数字转化为百分比的表达形式。math.random($limit: null): 返回0-1区间内的小数,如果加入参数limit则返回1至limit之间的整数,包括1和limit。
代码示例
@use 'sass:math';
.test {
math-ceil: math.ceil(15.2);
math-clamp: math.clamp(1, 0, -1);
math-clamp-one: math.clamp(10, 20, 30);
math-clamp-two: math.clamp(10, 66, 20);
math-floor: math.floor(15.8);
math-max: math.max(5, 7, 9, 0, -3, -7);
math-min: math.min(5, 7, 9, 0, -3, -7);
math-round: math.round(15.49);
math-round-one: math.round(15.5);
math-round-two: math.round(15.51);
math-abs: math.abs(-15);
math-pow: math.pow(2, 3);
math-sqrt: math.sqrt(3);
math-compatible: math.compatible(2, 1);
math-compatible-one: math.compatible(2px, 1px);
math-compatible-two: math.compatible(2, 1em);
math-compatible-three: math.compatible(2cm, 1 m);
math-compatible-four: math.compatible(2px, 1em);
math-is-unitless: math.is-unitless(100px);
math-is-unitless-one: math.is-unitless(100);
math-is-unitless-two: math.is-unitless(100em);
math-is-unitless-three: math.is-unitless(100 a);
math-unit: math.unit(100);
math-unit-one: math.unit(100px);
math-unit-two: math.unit(100 px2px);
math-unit-three: math.unit(100px * 2px);
math-unit-four: math.unit(100 em2s);
math-div: math.div(1, 2);
math-div-one: math.div(100px, 2px);
math-div-two: math.div(100px, 5);
math-div-twos: math.div(100em, 5);
//math-div-three: math.div(100px, 5s);
//math-div-four: math.div(100em, 5s);
math-percentage: math.percentage(0.66);
math-random: math.random();
math-random-one: math.random(2);
}
编译后的结果
.test {
math-ceil: 16;
math-clamp: 1;
math-clamp-one: 20;
math-clamp-two: 20;
math-floor: 15;
math-max: 9;
math-min: -7;
math-round: 15;
math-round-one: 16;
math-round-two: 16;
math-abs: 15;
math-pow: 8;
math-sqrt: 1.7320508076;
math-compatible: true;
math-compatible-one: true;
math-compatible-two: true;
math-compatible-three: false;
math-compatible-four: false;
math-is-unitless: false;
math-is-unitless-one: true;
math-is-unitless-two: false;
math-is-unitless-three: false;
math-unit: '';
math-unit-one: 'px';
math-unit-two: 'px2px';
math-unit-three: 'px*px';
math-unit-four: 'em2s';
math-div: 0.5;
math-div-one: 50;
math-div-two: 20px;
math-div-twos: 20em;
math-percentage: 66%;
math-random: 0.2758473244;
math-random-one: 1;
}

还没有留言,来发表第一条吧~