Skip to content

useClamp

Category
Export Size
214 B
Package
@vueuse/math
Last Changed
15 minutes ago

在两个其他值之间响应式地夹取一个值。

Demo

min: max: value:0

用法

ts
import { useClamp } from '@vueuse/math'

const min = ref(0)
const max = ref(10)
const value = useClamp(0, min, max)

你也可以传递一个 ref,当源 ref 发生变化时,返回的 computed 将会被更新:

ts
import { useClamp } from '@vueuse/math'

const number = ref(0)
const clamped = useClamp(number, 0, 10)

类型声明

typescript
export declare function useClamp(
  value: ReadonlyRefOrGetter<number>,
  min: MaybeRefOrGetter<number>,
  max: MaybeRefOrGetter<number>,
): ComputedRef<number>
export declare function useClamp(
  value: MaybeRefOrGetter<number>,
  min: MaybeRefOrGetter<number>,
  max: MaybeRefOrGetter<number>,
): Ref<number>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
ClemDee

变更日志

v10.0.0-beta.5 on 4/13/2023
cb644 - refactor!: remove isFunction and isString utils
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v9.13.0 on 2/18/2023
08a07 - feat: composable computed usage improvements (#2696)

Released under the MIT License.