跳到主要内容

useScreenOrientation

分类
导出大小
848 B
上次更改
3 个月前

响应式 屏幕方向 API。它为 Web 开发者提供有关用户当前屏幕方向的信息。

演示

为获得最佳效果,请使用移动或平板设备(或使用浏览器的原生检查器模拟方向更改)
isSupported: false
方向类型:
方向角度:0

用法

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"

屏幕方向 API MDN

类型声明

显示类型声明
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
>

来源

演示文档

贡献者

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

更新日志

v13.6.0
d32f8 - refactor: 为所有纯函数添加 @__NO_SIDE_EFFECTS__ 注释 (#4907)
v12.4.0
dd316 - feat: 尽可能在所有地方使用被动事件处理程序 (#4477)
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v10.8.0
a086e - fix: 更严格的类型
v10.7.0
be3cc - 修复:为 lock 和 unlock 添加容错 (#3575)

根据 MIT 许可证发布。