Copyright © 2025 World Wide Web Consortium. W3C® liability, trademark and permissive document license rules apply.
屏幕方向规范对设备屏幕方向的类型和角度进行了标准化,并提供了 锁定和解锁的方法。该规范定义的 API 会公开设备当前的 屏幕方向类型和角度,并在其变化时分派事件。这使得 Web 应用能够与 CSS 配合,以编程方式为 多种屏幕方向调整用户体验。该 API 尤其适用于诸如电脑游戏之类的应用, 在这类应用中,用户会物理地旋转设备,但屏幕方向本身 不应改变。该 API 对锁定屏幕方向进行了限制, 只有在满足某些 预锁定条件 时才允许。
本节描述了本文档在发布时的状态。最新的 W3C 发布列表及本技术报告的最新修订版可在 W3C 标准与草案索引中找到。
本文档仍在持续完善中。
本文档由 Web Applications Working Group 作为工作草案通过 推荐流程发布。
作为工作草案发布,并不代表 W3C 及其成员的认可。
本文档为草案,可能随时被更新、替换或废弃。除作为在研工作外,不适宜引用本文件。
本文档由遵循 W3C 专利政策 的工作组编制。 W3C 维护了 与本组成果相关的专利披露公开列表, 该页面还包括专利披露指引。任何个人若确知某项专利包含 必要声明, 必须按 W3C 专利政策第 6 节 披露相关信息。
本文档受 2025 年 8 月 18 日 W3C 工作流程文档 约束。
本节为非规范性内容。
在此示例中,选择“锁定”按钮会请求进入 全屏模式,然后将屏幕锁定为相反的方向。 选择“解锁”按钮会解锁屏幕。
<script>
function updateLockButton() {
const lockButton = document.getElementById("button");
const newOrientation = getOppositeOrientation();
lockButton.textContent = `Lock to ${newOrientation}`;
}
function getOppositeOrientation() {
return screen
.orientation
.type
.startsWith("portrait") ? "landscape" : "portrait";
}
async function rotate(lockButton) {
if (!document.fullscreenElement) {
await document.documentElement.requestFullscreen();
}
const newOrientation = getOppositeOrientation();
await screen.orientation.lock(newOrientation);
updateLockButton(lockButton);
}
screen.orientation.addEventListener("change", updateLockButton);
window.addEventListener("load", updateLockButton);
</script>
<button onclick="rotate(this)" id="button">
锁定到...
</button>
<button onclick="screen.orientation.unlock()">
解锁
</button>
锁定屏幕方向到某个 OrientationLockType orientation
意味着用户只能将屏幕旋转到特定的
屏幕方向 —— 可能会排除其他方向。屏幕可旋转到的方向由用户代理、用户偏好、操作系统的惯例或屏幕本身决定。例如,
将方向锁定为 landscape 意味着用户可以将屏幕旋转到 landscape-primary,如果系统允许,也许还能旋转到
landscape-secondary,但不会变为 portrait-secondary 方向。
解锁屏幕方向后,最终用户可以不受限制地将屏幕旋转到系统允许的任何 屏幕方向。
屏幕可以处于,或锁定为下列之一的 屏幕方向:
null)。该方向由设备的操作系统、用户代理、最终用户控制,或可能由
已安装的 Web 应用设置。例如,
当屏幕方向未锁定且用户旋转设备时,某些设备会将方向变化限制为
portrait-primary、
landscape-primary 和 landscape-secondary,
但不会变为
portrait-secondary。
输出设备的屏幕关联以下概念:
OrientationLockType
表示;如果未锁定则为
null。
OrientationType 表示。
下方的屏幕方向取值列表标准化了不同自然方向的屏幕,每种方向类型对应的角度:
Document 接口扩展了以下内部插槽:
| 内部插槽 | 描述 |
|---|---|
| [[orientationPendingPromise]] |
可能为 null,或为一个 Promise。当被赋值为 Promise 时,
该 promise 表示对屏幕方向锁定的请求。
|
WebIDLpartial interface Screen {
[SameObject] readonly attribute ScreenOrientation orientation;
};
Window 对象有一个 关联的
ScreenOrientation,
它是 Screen 的 orientation 对象(即
ScreenOrientation 实例,位于
window.screen.orientation)。
WebIDL[Exposed=Window]
interface ScreenOrientation : EventTarget {
Promise<undefined> lock(OrientationLockType orientation);
undefined unlock();
readonly attribute OrientationType type;
readonly attribute unsigned short angle;
attribute EventHandler onchange;
};
| 内部插槽 | 描述 |
|---|---|
| [[angle]] |
表示屏幕上次已知的当前方向角度(单位为度),类型为 unsigned short,值来源于
屏幕方向取值列表。
|
| [[initialType]] | 表示在浏览上下文创建时,屏幕的当前方向类型。 |
| [[type]] |
表示屏幕上次已知的当前方向类型,类型为OrientationType枚举值。
|
预锁定条件 是 用户代理 在允许锁定屏幕方向之前 MAY 施加的可选要求。 常见的预锁定条件包括要求文档处于全屏模式,或属于已安装的 Web 应用。参见 10. 与 Web 应用程序清单的交互 和 9. 与全屏 API 的交互 以获取具体示例。
当以 lock() 方法并传入
OrientationLockType
orientation 调用时,用户代理
MUST 执行以下步骤。
Document。
NotSupportedError"
DOMException,并中止这些步骤。
NotAllowedError"
DOMException,并中止这些步骤。
[[orientationPendingPromise]]
不为 null,则
拒绝并清空当前锁定的
promise
,其原因为 "AbortError"。
[[orientationPendingPromise]]
设为 一个新的 promise。
[[orientationPendingPromise]]。
当调用 unlock() 方法时,用户代理必须执行以下步骤:
Document。
null,则返回
undefined。
[[orientationPendingPromise]] 不为
null,则对 document 的当前锁定 promise 进行拒绝并置为 null,异常为
"AbortError"。
null 到 document。
通用安全检查针对 Document
document 的步骤如下:
InvalidStateError"
DOMException。
SecurityError" DOMException。
SecurityError" DOMException。
获取时,angle 属性返回 this 的
[[angle]]。
onchange 属性是一个 事件处理程序 IDL 属性,
用于 onchange 事件处理程序,其 事件处理类型
为 change。
WebIDLenum OrientationLockType {
"any",
"natural",
"landscape",
"portrait",
"portrait-primary",
"portrait-secondary",
"landscape-primary",
"landscape-secondary"
};
OrientationLockType 枚举
表示可以将屏幕锁定的屏幕方向。
any” 表示 任意。
natural” 表示 自然。
landscape” 表示 横屏。
portrait” 表示 竖屏。
portrait-primary” 表示 portrait-primary。
portrait-secondary” 表示 portrait-secondary。
landscape-primary” 表示 landscape-primary。
landscape-secondary” 表示
landscape-secondary。
WebIDLenum OrientationType {
"portrait-primary",
"portrait-secondary",
"landscape-primary",
"landscape-secondary"
};
OrientationType 枚举值用于表示屏幕的
当前方向类型。
ScreenOrientation。
[[initialType]]内部槽为屏幕的当前方向类型。
[[type]]内部槽为屏幕的当前方向类型。
[[angle]]内部槽为屏幕的当前方向角度。
当步骤要求用 DOMString
exceptionName 对 Document document 的
当前锁定 promise 进行拒绝并置为 null 时,
用户代理必须:
[[orientationPendingPromise]] 不为
null。
[[orientationPendingPromise]]。
DOMException
拒绝 promise。
[[orientationPendingPromise]] 设为
null。
当步骤要求将 OrientationLockType?
orientation 的方向锁定应用到 Document document 时,
用户代理必须执行以下步骤:
[[orientationPendingPromise]] 不为
null,则
拒绝并清空当前锁定的
promise,原因为
"AbortError"。
[[orientationPendingPromise]]
为 null,跳过。
AbortError"。
[[orientationPendingPromise]]
不为 null,则
拒绝并清空当前锁定的 promise,
原因为 "AbortError",并中止这些步骤。
null,
解锁屏幕方向。
null。
[[orientationPendingPromise]]
不为 null,
拒绝并清空当前锁定的
promise,
原因为 "NotSupportedError"。
当用户设置了阻止 Web 应用更改屏幕方向的偏好,或底层平台(而非用户代理)不允许锁定指定的 orientation 时可能发生以上情况。需注意,用户偏好或平台能力的差异可能会被用于指纹识别,因为它们可能导致锁定失败出现可检测模式。
[[orientationPendingPromise]]。
[[orientationPendingPromise]]
设为
null。
null,
以 undefined 解析
promise。
当用户代理判断某个顶层可遍历项的屏幕方向发生了变化, 或者用户将顶层浏览上下文移动到不同屏幕时, 应对该执行屏幕方向变更步骤, 目标是该顶层可遍历项的 活动文档。
屏幕方向变更步骤针对
Document document,具体如下:
ScreenOrientation。
[[type]]
且 angle 等于 screenOrientation 的
[[angle]],则中止这些步骤。
[HTML] 的“更新可见性状态”算法会运行屏幕方向变更步骤。
每当卸载文档清理步骤针对 document 运行时,用户代理必须执行以下步骤:
针对 Document document,完全解锁屏幕方向步骤如下:
[[orientationPendingPromise]] 不为
null,则
拒绝并清空当前锁定
promise,
原因为 "AbortError"。
null 到
topDocument。
用户代理MUST将 lock() 的使用限制为
简单全屏文档,作为预锁定条件。此要求可防止通过用户代理在方向锁定权限方面的行为差异进行指纹识别。
[fullscreen]
当文档退出全屏时,也会运行 完全解锁屏幕方向步骤。 [fullscreen]
Web Application Manifest 规范允许 Web 应用通过 默认屏幕方向 和 orientation 成员来设置默认屏幕方向。
用户代理应当要求已安装的 Web 应用以 "fullscreen" 显示模式展示,作为锁定前置条件。
由于用户的设备可能固定在一个方向上(例如安装在轮椅的扶手上),当开发人员希望用户在锁定屏幕方向时旋转设备时,开发人员需要了解Web 内容无障碍指南 (WCAG) 2.1中的方向成功标准。该标准要求无论屏幕方向如何,内容和功能必须可用。如果特定方向是必需的,Web 应用必须告知用户方向要求。
屏幕的 类型 和 角度 是潜在的指纹识别向量。以下缓解措施有助于保护用户隐私,不暴露设备被持有的方式,同时防止 次要方向类型及其相关角度被用于指纹识别。
为保护用户隐私,方向变更事件的传递受到多项限制:
对 文档 要求是 具有用户注意的顶层可遍历项的完全激活后代 ,确保方向事件仅发送到既在系统层面可见又获得用户关注(如有焦点或能接受键盘输入)的窗口中的文档。额外的可见性状态检查进一步增强安全性。这些限制可防止后台标签页、隐藏窗口和最小化应用收集方向数据以进行指纹识别。
为了抵御指纹识别,用户代理SHOULD实施以下保护措施,特别是在注重隐私的环境如隐私浏览模式下:
[[initialType]]。
type
getter 的可能返回值限制为
"portrait-primary"
或
"landscape-primary"。
返回值由屏幕宽高比决定。
[[initialType]]
相同,则
angle
getter 返回 0,否则返回 90。
除了被标记为非规范性的章节外,本规范中的所有创作指南、图示、示例和注释均为非规范性内容。除此之外,规范中的其他内容均为规范性内容。
本文档中的关键词 MAY、MUST 和 SHOULD 应按照 BCP 14 [RFC2119] [RFC8174] 的描述进行解释,并且仅当这些词全部大写时才具有上述意义,如本例所示。
WebIDLpartial interface Screen {
[SameObject] readonly attribute ScreenOrientation orientation;
};
[Exposed=Window]
interface ScreenOrientation : EventTarget {
Promise<undefined> lock(OrientationLockType orientation);
undefined unlock();
readonly attribute OrientationType type;
readonly attribute unsigned short angle;
attribute EventHandler onchange;
};
enum OrientationLockType {
"any",
"natural",
"landscape",
"portrait",
"portrait-primary",
"portrait-secondary",
"landscape-primary",
"landscape-secondary"
};
enum OrientationType {
"portrait-primary",
"portrait-secondary",
"landscape-primary",
"landscape-secondary"
};
angle
属性(ScreenOrientation)
§5.
[[angle]]
内部槽(ScreenOrientation)
§5.1
"any"
枚举值(OrientationLockType)
§6.
[[initialType]]
内部槽(ScreenOrientation)
§5.1
"landscape"
枚举值(OrientationLockType)
§6.
lock
方法(ScreenOrientation)
§5.
"natural"
枚举值(OrientationLockType)
§6.
onchange
属性(ScreenOrientation)
§5.
orientation
属性(Screen)
§4.
OrientationLockType 枚举
§6.
[[orientationPendingPromise]]
内部槽(Document)
§3.1
OrientationType 枚举
§7.
"portrait"
枚举值(OrientationLockType)
§6.
ScreenOrientation 接口
§5.
type
属性(ScreenOrientation)
§5.
[[type]] 内部槽(ScreenOrientation)
§5.1
unlock
方法(ScreenOrientation)
§5.
manifest)
Screen
接口
Document 接口
EventTarget 接口
Document)
EventHandler
Document 的完全激活
Document 的具有用户注意的顶层可遍历项的完全激活后代
Document 的可见性状态
Window
接口
list)
AbortError 异常
DOMException 接口
DOMString 接口
[Exposed] 扩展属性
InvalidStateError 异常
NotAllowedError 异常
NotSupportedError 异常
Promise 接口
[SameObject] 扩展属性
SecurityError 异常
exception)
undefined 类型
unsigned short 类型
感谢 Christophe Dumez、Anne van Kesteren、Chundong Wang、Fuqiao Xue 和 Chaals McCathie Nevile 提出的有用意见。
特别感谢 Chris Jones 和 Jonas Sicking 对该 API 最初设计的贡献。
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in: