Copyright © 2023 World Wide Web Consortium. W3C® liability, trademark and permissive document license rules apply.
屏幕方向规范标准化了设备屏幕方向的类型和角度,并提供了锁定和解锁屏幕方向的方式。该规范定义的API暴露了设备屏幕方向的当前类型和角度,并在变化时触发事件。这使得Web应用程序能够以编程方式适应多种屏幕方向的用户体验,与CSS协同工作。该API还允许在某些前提条件下锁定屏幕方向。这对于用户物理旋转设备但屏幕方向不应改变的应用程序(例如电脑游戏)特别有用。
本节描述了本文档在发布时的状态。当前W3C出版物和此技术报告的最新修订版列表可在 W3C技术报告索引 中找到 https://www.w3.org/TR/。
本文档是一个正在进行的工作。
本文档由Web应用程序工作组发布为工作草案,使用 推荐轨道。
发布为工作草案并不意味着W3C及其成员的认可。
这是一个草案文件,可能会随时更新、替换或由其他文件取代。因此,不适合将本文档引用为其他用途,除非作为正在进行的工作。
本文档由一个在 W3C 专利政策下运作的团体制作。 W3C维护了 与该团体交付物相关的任何专利披露的公共列表; 该页面还包括披露专利的说明。知晓含有必要专利声明的个人必须根据 W3C专利政策第6节披露信息。
本文档受2023年6月12日W3C流程文档管辖。
本节为非规范性内容。
在此示例中,点击“锁定”按钮请求进入全屏模式,然后将屏幕锁定为相反方向。点击“解锁”按钮则解锁屏幕。
<script>
function updateLockButton() {
const lockButton = document.getElementById("button");
const newOrientation = getOppositeOrientation();
lockButton.textContent = `锁定为 ${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
方向
意味着用户只能将屏幕旋转到特定的
屏幕方向,可能会排除其他方向。可以旋转屏幕的可能方向由用户代理、用户偏好、操作系统的惯例或屏幕本身决定。例如,将方向锁定为
横向意味着用户可以将屏幕旋转到
横向-主方向,系统允许的情况下,可能还可以旋转到
横向-次方向,但不会变为
纵向-次方向。
解锁屏幕方向 后,最终用户可以根据系统允许的任何 屏幕方向自由旋转屏幕。
屏幕可以处于以下任意 屏幕方向之一,或锁定在其中一种:
null
)。该方向由设备操作系统、用户代理或最终用户决定,或由
已安装的Web应用程序设置。例如,
当屏幕方向未锁定且用户旋转设备时,一些设备将方向限制为
纵向-主方向、
横向-主方向,以及
横向-次方向,但不会旋转到
纵向-次方向。
输出设备的屏幕具有以下相关概念:
OrientationLockType
,
即屏幕被锁定到的方向,或者在
解锁时为
null
。
OrientationType
。
下面的屏幕方向值列表标准化了具有不同 自然方向的屏幕与每种屏幕方向类型相关联的角度:
Document
接口扩展了以下内部槽:
内部槽 | 描述 |
---|---|
[[orientationPendingPromise]] |
可能是null 或Promise 。当分配给一个
Promise 时,
该Promise表示请求锁定屏幕方向。
|
WebIDLpartial interface Screen {
[SameObject] readonly attribute ScreenOrientation
orientation
;
};
Window
对象具有一个关联的
ScreenOrientation
,
它是Screen
的orientation
对象(即
window.screen.orientation
处的
ScreenOrientation
实例)。
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
枚举值表示。
|
当调用lock
()
方法并传递
OrientationLockType
orientation时,用户代理 必须执行以下步骤。
用户代理 可以要求 文档 及其关联的 浏览上下文满足一个或多个 预锁定条件,以便 锁定屏幕方向。参见 10. 与Web应用程序清单的交互和 9. 与全屏API的交互。
Document
。
NotSupportedError
"
DOMException
,中止这些步骤。
[[orientationPendingPromise]]
不为null
,则拒绝并清除当前的锁定promise,
并抛出
"AbortError
"。
[[orientationPendingPromise]]
设置为
一个新的promise。
[[orientationPendingPromise]]
。
当调用
unlock
()
方法时,用户代理 必须执行以下步骤:
Document
。
null
,返回undefined
。
[[orientationPendingPromise]]
不为null
,
拒绝并清除当前的锁定promise,
并抛出
"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
枚举
表示可以将屏幕锁定的屏幕方向。
WebIDLenum OrientationType
{
"portrait-primary
",
"portrait-secondary
",
"landscape-primary
",
"landscape-secondary
"
};
OrientationType
枚举值用于表示屏幕的
当前方向类型。
当创建一个浏览上下文context时, 用户代理必须执行以下步骤:
ScreenOrientation
。
[[initialType]]
内部槽为屏幕的当前方向类型。
[[type]]
内部槽为屏幕的当前方向类型。
[[angle]]
内部槽为屏幕的当前方向角度。
当需要执行步骤来拒绝并使当前的锁定承诺无效时,
对于Document
document使用DOMString
exceptionName,
用户代理必须:
[[orientationPendingPromise]]
不为null
。
[[orientationPendingPromise]]
。
DOMException
。
[[orientationPendingPromise]]
设置为null
。
当步骤要求对OrientationLockType?
orientation的屏幕锁定应用于Document
document时,
用户代理必须执行以下步骤:
[[orientationPendingPromise]]
不为null
,
则拒绝并使当前的锁定承诺无效,并抛出"AbortError
"。
[[orientationPendingPromise]]
为null
,则继续。
AbortError
"。
[[orientationPendingPromise]]
不为null
,则拒绝并使当前的锁定承诺无效,并抛出"AbortError
",并终止这些步骤。
null
,则解锁屏幕方向。
null
。
[[orientationPendingPromise]]
不为null
,则拒绝并使当前的锁定承诺无效,并抛出"NotSupportedError
"。
如果用户设置了阻止Web应用程序更改屏幕方向的偏好,或底层平台而不是用户代理不允许锁定屏幕方向到指定的orientation,这种情况可能发生。
[[orientationPendingPromise]]
。
[[orientationPendingPromise]]
设置为null
。
null
,则解析promise为undefined
。
当用户代理确定屏幕方向已针对顶级浏览上下文更改,或用户将顶级浏览上下文移动到不同屏幕时, 执行屏幕方向更改步骤,与顶级浏览上下文的活动文档一起。
针对Document
document的
屏幕方向更改步骤如下:
ScreenOrientation
。
[[type]]
,并且angle等于screenOrientation的[[angle]]
,则中止这些步骤。
每当卸载文档清理步骤在一个document上执行时,用户代理必须执行以下步骤:
针对Document
document的完全解锁屏幕方向步骤如下:
[[orientationPendingPromise]]
不为null
,则使用"AbortError
"拒绝并将当前锁定的承诺置空。
null
。
用户代理应该将lock
()
的使用限制为简单的全屏文档,作为预锁定条件。[fullscreen]
当文档退出全屏时,它还会运行完全解锁屏幕方向步骤。[fullscreen]
Web 应用清单规范允许 Web 应用通过默认屏幕方向设置方向成员。
用户代理应该要求已安装的 Web 应用以“全屏”显示模式呈现,作为预锁定条件。
由于用户的设备可能固定在一个方向上(例如安装在轮椅的扶手上),当开发人员希望用户在锁定屏幕方向时旋转设备时,开发人员需要了解Web 内容无障碍指南 (WCAG) 2.1中的方向成功标准。该标准要求无论屏幕方向如何,内容和功能必须可用。如果特定方向是必需的,Web 应用必须告知用户方向要求。
屏幕的类型和角度是潜在的指纹识别向量。以下缓解措施有助于保护用户的隐私,防止透露设备的持握方式,并防止使用次要方向类型及其相关角度进行指纹识别。
为抵制指纹识别(例如在隐私浏览中),用户代理可以:
[[initialType]]
。
type
获取器的返回值限制为"portrait-primary
"或"landscape-primary
"。屏幕的宽高比决定返回哪个值。
[[initialType]]
匹配,则angle
获取器返回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
)
Window
接口
list
)
AbortError
异常
DOMException
接口
DOMString
接口
[Exposed]
扩展属性
InvalidStateError
异常
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:
Referenced in: