◆

Sass math 数学函数用法大全

2023-02-02 01:370

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;
}
💬留言板0

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

PAGE LOAD / LIVESass math 数学函数用法大全加载中