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

浏览器兼容性

另见