Skip to content

useDeviceMotion

反应式 DeviceMotionEvent。为 Web 开发者提供有关设备位置和方向变化速度的信息。

¥Reactive DeviceMotionEvent. Provide web developers with information about the speed of changes for the device's position and orientation.

示例

Device Motion:
{
  "acceleration": {
    "x": null,
    "y": null,
    "z": null
  },
  "accelerationIncludingGravity": {
    "x": null,
    "y": null,
    "z": null
  },
  "rotationRate": {
    "alpha": null,
    "beta": null,
    "gamma": null
  },
  "interval": 0
}

用法

¥Usage

js
import { useDeviceMotion } from '@vueuse/core'

const {
  acceleration,
  accelerationIncludingGravity,
  rotationRate,
  interval,
} = useDeviceMotion()
状态类型描述
accelerationobject给出设备在 X、Y 和 Z 三个轴上的加速度的对象。
accelerationIncludingGravityobject在重力作用下给出设备在 X、Y 和 Z 三个轴上的加速度的对象。
rotationRateobject给出设备在三个方向轴 alpha、beta 和 gamma 上的方向变化率的对象。
intervalNumber表示从设备获取数据的时间间隔(以毫秒为单位)的数字。

你可以找到 有关 MDN 上的状态的更多信息

¥You can find more information about the state on the MDN.

组件用法

¥Component Usage

vue
<template>
  <UseDeviceMotion v-slot="{ acceleration }">
    Acceleration: {{ acceleration }}
  </UseDeviceMotion>
</template>

类型声明

typescript
export interface DeviceMotionOptions
  extends ConfigurableWindow,
    ConfigurableEventFilter {}
/**
 * Reactive DeviceMotionEvent.
 *
 * @see https://vueuse.org/useDeviceMotion
 * @param options
 */
export declare function useDeviceMotion(options?: DeviceMotionOptions): {
  acceleration: Ref<DeviceMotionEventAcceleration | null>
  accelerationIncludingGravity: Ref<DeviceMotionEventAcceleration | null>
  rotationRate: Ref<DeviceMotionEventRotationRate | null>
  interval: Ref<number>
}
export type UseDeviceMotionReturn = ReturnType<typeof useDeviceMotion>

源代码

源代码示例文档

变更日志

No recent changes

VueUse 中文网 - 粤ICP备13048890号