Skip to content

useClamp

反应式性地将一个值钳位在两个其他值之间。

¥Reactively clamp a value between two other values.

示例

min: max: value:0

用法

¥Usage

ts
import { 
useClamp
} from '@vueuse/math'
const
min
=
shallowRef
(0)
const
max
=
shallowRef
(10)
const
value
=
useClamp
(0,
min
,
max
)

你还可以传递 ref,当源引用更改时,返回的 computed 将更新:

¥You can also pass a ref and the returned computed will be updated when the source ref changes:

ts
import { 
useClamp
} from '@vueuse/math'
const
number
=
shallowRef
(0)
const
clamped
=
useClamp
(
number
, 0, 10)