useScreenOrientation
響應式 螢幕方向 API。它為網頁開發者提供有關使用者目前螢幕方向的資訊。
範例
用法
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"
其中之一。
類型宣告
顯示類型宣告
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>