DevicePosture: change 事件

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

当设备的姿态发生变化时,DevicePosture 接口的 change 事件就会触发。例如,当一个可折叠设备从 folded 姿态变为 continuous 姿态时。

语法

在诸如 addEventListener() 之类的方法中使用事件名称,或设置事件处理程序属性。

js
addEventListener("change", (event) => { })

onchange = (event) => { }

事件类型

一个通用的 Event

示例

js
const postureOutput = document.getElementById("currentPosture");

function reportPostureOutput() {
  // type property returns "continuous" or "folded"
  postureOutput.textContent = `Device posture: ${navigator.devicePosture.type}`;
}

navigator.devicePosture.addEventListener("change", reportPostureOutput);

规范

规范
设备姿态 API
# dom-deviceposture-onchange

浏览器兼容性

另见