DeviceOrientationEvent

基線 2023 *
新推出

自 2023 年 9 月起,此功能可在最新的裝置和瀏覽器版本上使用。此功能可能無法在較舊的裝置或瀏覽器上使用。

* 此特性的某些部分可能存在不同級別的支援。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

DeviceOrientationEvent 介面是 裝置方向事件 的一部分,它為 Web 開發者提供有關執行網頁的裝置的物理方向的資訊。

Event DeviceOrientationEvent

建構函式

DeviceOrientationEvent.DeviceOrientationEvent()

建立一個新的 DeviceOrientationEvent

例項屬性

DeviceOrientationEvent.absolute 只讀

一個布林值,指示裝置是否正在絕對地提供方向資料。

DeviceOrientationEvent.alpha 只讀

一個數字,表示裝置繞 z 軸的運動,以度為單位,範圍從 0(包含)到 360(不包含)。

DeviceOrientationEvent.beta 只讀

一個數字,表示裝置繞 x 軸的運動,以度為單位,範圍從 -180(包含)到 180(不包含)。這代表裝置的前後運動。

DeviceOrientationEvent.gamma 只讀

一個數字,表示裝置繞 y 軸的運動,以度為單位,範圍從 -90(包含)到 90(不包含)。這代表裝置的左右運動。

DeviceOrientationEvent.webkitCompassHeading 非標準 只讀

一個數字,表示世界座標系下裝置繞 z 軸運動與北方方向之間的差異,以度為單位,範圍從 0 到 360。

DeviceOrientationEvent.webkitCompassAccuracy 非標準 只讀

指南針的精度表示偏差是正的還是負的。通常為 10。

示例

js
window.addEventListener("deviceorientation", (event) => {
  console.log(`${event.alpha} : ${event.beta} : ${event.gamma}`);
});

規範

規範
裝置方向和運動
# deviceorientation

瀏覽器相容性

另見