round()
将 Number
四舍五入到指定的小数位数,或创建一个带有预定义 *decimalLength* 参数的四舍五入 Function
。
const roundedValue = utils.round(value, decimalLength);
const roundingFunction = utils.round(decimalLength);
参数
名称 | 接受 |
---|---|
value (可选) | 数字 |
小数位数 | 数字 |
返回
如果提供了值,则返回 Number
,否则返回一个 链式实用工具 Function
,用于将数字四舍五入到指定的小数位数
const clampAndRound = utils.clamp(0, 100).round(2); // Clamp then round to 2 decimal places
clampAndRound(72.7523); // 72.75
clampAndRound(120.2514); // 100
round() 代码示例
import { animate, utils } from 'animejs';
animate('.normal', {
rotate: '1turn',
duration: 3000,
loop: true,
});
animate('.rounded', {
rotate: '1turn',
modifier: utils.round(1), // Used as a function
duration: 3000,
loop: true,
});
<div class="x-large spaced-evenly row">
<div class="col">
<div class="clock normal"></div>
<div class="label">normal</div>
</div>
<div class="col">
<div class="clock rounded"></div>
<div class="label">rounded (.1)</div>
</div>
</div>