合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

15 lines
554 B

  1. // @ts-nocheck
  2. /**
  3. *
  4. * @param val
  5. * @param min
  6. * @param max
  7. * @returns
  8. */
  9. export function clamp(val: number, min: number, max: number): number {
  10. return Math.max(min, Math.min(max, val));
  11. }
  12. // console.log(clamp(5 ,0, 10)); // 输出: 5(在范围内,不做更改)
  13. // console.log(clamp(-5 ,0, 10)); // 输出: 0(小于最小值,被限制为最小值)
  14. // console.log(clamp(15 ,0, 10)); // 输出: 10(大于最大值,被限制为最大值)