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

瀏覽器相容性

另見