跳到主要內容

useScreenOrientation

類別
導出大小
770 B
上次變更
3 週前

響應式 螢幕方向 API。它為網頁開發者提供有關使用者目前螢幕方向的資訊。

範例

為了獲得最佳效果,請使用手機或平板裝置(或使用瀏覽器的原生檢查器來模擬方向變更)
isSupported: false
方向類型:
方向角度: 0

用法

ts
import { useScreenOrientation } from '@vueuse/core'

const {
  isSupported,
  orientation,
  angle,
  lockOrientation,
  unlockOrientation,
} = useScreenOrientation()

若要鎖定方向,您可以將 OrientationLockType 傳遞給 lockOrientation 函數

ts
import { useScreenOrientation } from '@vueuse/core'

const {
  isSupported,
  orientation,
  angle,
  lockOrientation,
  unlockOrientation,
} = useScreenOrientation()

lockOrientation('portrait-primary')

然後再次解鎖,使用以下方式

ts
unlockOrientation()

接受的方向類型為 "landscape-primary""landscape-secondary""portrait-primary""portrait-secondary""any""landscape""natural""portrait" 其中之一。

螢幕方向 API MDN

類型宣告

顯示類型宣告
typescript
export type OrientationType =
  | "portrait-primary"
  | "portrait-secondary"
  | "landscape-primary"
  | "landscape-secondary"
export type OrientationLockType =
  | "any"
  | "natural"
  | "landscape"
  | "portrait"
  | "portrait-primary"
  | "portrait-secondary"
  | "landscape-primary"
  | "landscape-secondary"
export interface ScreenOrientation extends EventTarget {
  lock: (orientation: OrientationLockType) => Promise<void>
  unlock: () => void
  readonly type: OrientationType
  readonly angle: number
  addEventListener: (
    type: "change",
    listener: (this: this, ev: Event) => any,
    useCapture?: boolean,
  ) => void
}
/**
 * Reactive screen orientation
 *
 * @see https://vueuse.dev.org.tw/useScreenOrientation
 */
export declare function useScreenOrientation(options?: ConfigurableWindow): {
  isSupported: ComputedRef<boolean>
  orientation: Ref<OrientationType | undefined, OrientationType | undefined>
  angle: ShallowRef<number, number>
  lockOrientation: (type: OrientationLockType) => Promise<void>
  unlockOrientation: () => void
}
export type UseScreenOrientationReturn = ReturnType<typeof useScreenOrientation>

原始碼

原始碼範例文件

貢獻者

Anthony Fu
Anthony Fu
Michael J. Roberts
IlyaL
Fernando Fernández
Satrong
JunaYa
Jelf
Nicolas Hedger

更新日誌

v12.4.0 於 2025/1/10
dd316 - feat: 盡可能在任何地方使用被動事件處理器 (#4477)
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,最佳化 bundles 並清理程式碼 (#4349)
v10.8.0 於 2024/2/20
a086e - fix: 更嚴格的類型
v10.7.0 於 2023/12/5
be3cc - fix: 為鎖定和解鎖新增容錯能力 (#3575)

在 MIT 授權下發布。