ScreenOrientation
Baseline 广泛可用 *
ScreenOrientation 接口是 屏幕方向 API 的一部分,它提供了关于文档当前方向的信息。
可以通过 screen.orientation 属性来获取 ScreenOrientation 实例对象。
实例属性
ScreenOrientation.type只读-
返回文档的当前方向类型,可以是
portrait-primary(竖屏主方向)、portrait-secondary(竖屏副方向)、landscape-primary(横屏主方向)或landscape-secondary(横屏副方向)之一。 ScreenOrientation.angle只读-
返回文档的当前方向角度。
实例方法
ScreenOrientation.lock()-
将包含文档的方向锁定为其默认方向,并返回一个
Promise。 ScreenOrientation.unlock()-
解锁包含文档的方向,使其恢复为默认方向。
事件
使用 addEventListener() 或通过将事件监听器分配给此接口的 oneventname 属性来监听这些事件。
change-
每当屏幕方向发生变化时都会触发。
示例
在以下示例中,我们监听方向 change 事件,并记录新的 屏幕方向类型和 角度。
js
screen.orientation.addEventListener("change", (event) => {
const type = event.target.type;
const angle = event.target.angle;
console.log(`ScreenOrientation change: ${type}, ${angle} degrees.`);
});
规范
| 规范 |
|---|
| 屏幕方向 # screenorientation-interface |
浏览器兼容性
加载中…