Screen: orientation 属性

Baseline 已广泛支持

此功能已成熟,并可在许多设备和浏览器版本上运行。自 2023 年 3 月以来,它已在各种浏览器中可用。

Screen 接口的只读属性 orientation 返回屏幕的当前方向。

一个 ScreenOrientation 实例,表示屏幕的方向。

请注意,较旧的、带有前缀的版本返回一个字符串,该字符串等同于 ScreenOrientation.type

示例

js
switch (screen.orientation.type) {
  case "landscape-primary":
    console.log("That looks good.");
    break;
  case "landscape-secondary":
    console.log("Mmm… the screen is upside down!");
    break;
  case "portrait-secondary":
  case "portrait-primary":
    console.log("Mmm… you should rotate your device to landscape");
    break;
  default:
    console.log("The orientation API isn't supported in this browser :(");
}

规范

规范
屏幕方向
# dom-screen-orientation

浏览器兼容性

另见