Window: deviceorientation 事件
當裝置的方向感測器獲取到裝置相對於地球座標系的新方向資料時,deviceorientation 事件會被觸發。這些資料是從裝置內部的磁力計收集的。
詳情請參閱方向和運動資料解說。
此事件不可取消,也不會冒泡。
語法
在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。
js
addEventListener("deviceorientation", (event) => { })
ondeviceorientation = (event) => { }
事件型別
一個 DeviceOrientationEvent。繼承自 Event。
事件屬性
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
if (window.DeviceOrientationEvent) {
window.addEventListener(
"deviceorientation",
(event) => {
const rotateDegrees = event.alpha; // alpha: rotation around z-axis
const leftToRight = event.gamma; // gamma: left to right
const frontToBack = event.beta; // beta: front back motion
handleOrientationEvent(frontToBack, leftToRight, rotateDegrees);
},
true,
);
}
function handleOrientationEvent(frontToBack, leftToRight, rotateDegrees) {
// do something amazing
}
規範
| 規範 |
|---|
| 裝置方向和運動 # deviceorientation |
| 裝置方向和運動 # dom-window-ondeviceorientation |
瀏覽器相容性
載入中…