Skip to content

useDeviceOrientation ​

Category
Export Size
731 B
Last Changed
4 months ago

响应式 DeviceOrientationEvent。为 Web 开发者提供来自运行网页的设备的物理方向信息。

Demo ​

isSupported: false
isAbsolute: false
alpha: null
beta: null
gamma: null

用法 ​

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

const {
  isAbsolute,
  alpha,
  beta,
  gamma,
} = useDeviceOrientation()
状态类型描述
isAbsoluteboolean一个布尔值,指示设备是否绝对提供方向数据。
alphanumber一个表示设备围绕 z 轴运动的数字,以度为单位,值范围从 0 到 360。
betanumber一个表示设备围绕 x 轴运动的数字,以度为单位,值范围从 -180 到 180。
gammanumber一个表示设备围绕 y 轴运动的数字,以度为单位,值范围从 -90 到 90。

您可以在 MDN 上找到有关状态的更多信息。

组件用法 ​

vue
<template>
  <UseDeviceOrientation v-slot="{ alpha, beta, gamma }">
    Alpha: {{ alpha }}
    Beta: {{ beta }}
    Gamma: {{ gamma }}
  </UseDeviceOrientation>
</template>

Type Declarations ​

typescript
/**
 * Reactive DeviceOrientationEvent.
 *
 * @see https://vueuse.org/useDeviceOrientation
 * @param options
 */
export declare function useDeviceOrientation(options?: ConfigurableWindow): {
  isSupported: ComputedRef<boolean>
  isAbsolute: ShallowRef<boolean, boolean>
  alpha: Ref<number | null, number | null>
  beta: Ref<number | null, number | null>
  gamma: Ref<number | null, number | null>
}
export type UseDeviceOrientationReturn = ReturnType<typeof useDeviceOrientation>

Source ​

Source • Demo • Docs

Contributors ​

Anthony Fu
丶远方
Antério Vieira
Anthony Fu
Guspan Tanadi
Fernando Fernández
vaakian X
Jelf
Shinigami
wheat
Alex Kozack

Changelog ​

dd316 - feat: use passive event handlers everywhere is possible (#4477)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)

Released under the MIT License.