useScreenOrientation
响应式 屏幕方向 API。它为 Web 开发者提供有关用户当前屏幕方向的信息。
演示
用法
ts
import { useScreenOrientation } from '@vueuse/core'
const {
isSupported,
orientation,
angle,
lockOrientation,
unlockOrientation,
} = useScreenOrientation()要锁定方向,您可以向 lockOrientation 函数传递一个 OrientationLockType
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"。
类型声明
显示类型声明
ts
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.org.cn/useScreenOrientation
*
* @__NO_SIDE_EFFECTS__
*/
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>