跳到主要內容

useDeviceOrientation

類別
導出大小
731 B
上次變更
3 週前

響應式 DeviceOrientationEvent。為網頁開發者提供來自執行網頁之裝置物理方向的資訊。

示範

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

用法

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

const {
  isAbsolute,
  alpha,
  beta,
  gamma,
} = useDeviceOrientation()
狀態類型描述
isAbsolute布林值一個布林值,表示裝置是否絕對地提供方向數據。
alpha數字一個數字,表示裝置繞 z 軸的運動,以度為單位表示,值範圍從 0 到 360。
beta數字一個數字,表示裝置繞 x 軸的運動,以度為單位表示,值範圍從 -180 到 180。
gamma數字一個數字,表示裝置繞 y 軸的運動,以度為單位表示,值範圍從 -90 到 90。

您可以在 MDN 上找到關於狀態的更多資訊

元件用法

此函式也透過 @vueuse/components 套件提供無渲染元件版本。了解更多關於用法的資訊

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

類型宣告

typescript
/**
 * Reactive DeviceOrientationEvent.
 *
 * @see https://vueuse.dev.org.tw/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>

原始碼

原始碼示範文件

貢獻者

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

變更日誌

v12.4.0 於 2025/1/10
dd316 - feat: 盡可能在任何地方使用被動事件處理器 (#4477)
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,優化捆綁包並清理 (#4349)

在 MIT 許可證下發布。