Skip to content

useVibrate

类别
导出体积
700 B
上次更改
2 months ago

响应式振动 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 {
  /**
   *
   * 一个值数组描述了设备震动和不震动的交替周期。
   * 数组中的每个值都被转换为整数,然后交替解释为设备应该震动的毫秒数和不应该震动的毫秒数。
   *
   * @default []
   *
   */
  
pattern
?:
MaybeRefOrGetter
<number[] | number>
/** * 持续震动的间隔,以毫秒为单位 * * 设置为 `0` 表示禁用 * * @default 0 * */
interval
?: number
} /** * 响应式震动 * * @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): {
isSupported
:
ComputedRef
<boolean>
pattern
:
MaybeRefOrGetter
<number | number[]>
intervalControls
:
Pausable
| undefined
vibrate
: (
pattern
?: number | number[]) => void
stop
: () => void
} export type
UseVibrateReturn
=
ReturnType
<typeof
useVibrate
>

源码

源码演示文档

贡献者

一纸忘忧
Anthony Fu
SerKo
Arthur Darkstone
IlyaL
Anthony Fu
Jelf
Mangokk
Michael J. Roberts

更新日志

d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)