useVibrate
响应式振动 API
大多数现代移动设备都包含振动硬件,使软件能够通过使设备震动来向用户提供物理反馈。
振动 API 为 Web 应用程序提供了访问此硬件的能力,如果设备支持该功能,则允许使用它,如果设备不支持,则不执行任何操作。
示例
Your browser does not support the Vibration API
用法
振动被描述为一种开关脉冲的模式,可能具有不同的长度。
该模式可以是一个描述以毫秒为单位的振动时间的整数,也可以是一个描述振动和暂停模式的整数数组。
ts
import { useVibrate } from '@vueuse/core'
// 这会让设备振动 300 毫秒
// 然后在振动设备另外 300 毫秒之前暂停 100 毫秒:
const { vibrate, stop, isSupported } = useVibrate({ pattern: [300, 100, 300] })
// 开始振动,当模式完成时,它将自动停止:
vibrate()
// 但是如果你想停止它,你可以:
stop()类型声明
ts
export interface UseVibrateOptions
extends ConfigurableNavigator, ConfigurableScheduler {
/**
*
* 一个值数组描述了设备震动和不震动的交替周期。
* 数组中的每个值都被转换为整数,然后交替解释为设备应该震动的毫秒数和不应该震动的毫秒数。
*
* @default []
*
*/
pattern?: MaybeRefOrGetter<Arrayable<number>>
/**
* 持续震动的间隔,以毫秒为单位
*
* 设置为 `0` 表示禁用
*
* @deprecated Please use `scheduler` option instead
* @default 0
*
*/
interval: number
}
export interface UseVibrateReturn extends Supportable {
pattern: MaybeRefOrGetter<Arrayable<number>>
intervalControls?: Pausable
vibrate: (pattern?: Arrayable<number>) => void
stop: () => void
}
/**
* 响应式震动
*
* @see https://vueuse.org/useVibrate
* @see https://developer.mozilla.org/en-US/docs/Web/API/Vibration_API
* @param options
*
* @__NO_SIDE_EFFECTS__
*/
export declare function useVibrate(
options?: UseVibrateOptions,
): UseVibrateReturn