页面可见性 API
使用选项卡式浏览,任何给定网页都有可能在后台,因此对用户不可见。页面可见性 API 提供了你可以观察的事件,以便了解文档何时可见或隐藏,以及查看页面当前可见性状态的功能。
备注: 页面可见性 API 对于节省资源和提高性能特别有用,它使页面在文档不可见时避免执行不必要的任务。
当用户最小化窗口或切换到另一个选项卡时,API 会发送visibilitychange
事件,让监听者知道页面状态已更改。你可以检测事件并执行某些操作或行为不同。例如,如果你的网络应用正在播放视频,则可以在用户将标签放入背景时暂停视频,并在用户返回标签时恢复播放。用户不会在视频中丢失位置,视频的音轨不会干扰新前景选项卡中的音频,并且用户在此期间不会错过任何视频。
<iframe>
的可见性状态与父文档相同。使用 CSS 属性(例如display: none;
)隐藏<iframe>
不会触发可见性事件或更改框架中包含的文档的状态。
使用情景
制定有助于后台页面性能的策略
在页面可见性 API 之外,用户代理会采取许多策略来减轻后台或隐藏选项卡对性能的影响。这些可能包括:
- 大多数浏览器不会调用被隐藏的标签页或
<iframe>
框架当中requestAnimationFrame()
定义的回调函数,这会提升性能并且延长电池的使用寿命。 - 在后台标签页或不活跃的标签页中
setTimeout()
等定时器会受到一定的限制以提升性能。参见实际延时比设定值更久的原因。 - Budget-based background timeout throttling is now available in modern browsers (Firefox 58+, Chrome 57+), placing an additional limit on background timer CPU usage. This operates in a similar way across modern browsers, with the details being as follows:
- In Firefox, windows in background tabs each have their own time budget in milliseconds — a max and a min value of +50 ms and -150 ms, respectively. Chrome is very similar except that the budget is specified in seconds.
- Windows are subjected to throttling after 30 seconds, with the same throttling delay rules as specified for window timers (again, see Reasons for delays longer than specified). In Chrome, this value is 10 seconds.
- Timer tasks are only permitted when the budget is non-negative.
- Once a timer's code has finished running, the duration of time it took to execute is subtracted from its window's timeout budget.
- The budget regenerates at a rate of 10 ms per second, in both Firefox and Chrome.
Some processes are exempt from this throttling behavior. In these cases, you can use the Page Visibility API to reduce the tabs' performance impact while they're hidden.
- Tabs which are playing audio are considered foreground and aren't throttled.
- Tabs running code that's using real-time network connections (WebSockets and WebRTC) go unthrottled in order to avoid closing these connections timing out and getting unexpectedly closed.
- IndexedDB processes are also left unthrottled in order to avoid timeouts.
示例
看一个 在线的例子(带声音的视频)
在此例中,当你切换到另一个标签时视频会暂停,当你返回到当前标签时视频会再次播放,代码如下:
js
// 设置隐藏属性和改变可见属性的事件的名称
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") {
// Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
var videoElement = document.getElementById("videoElement");
// 如果页面是隐藏状态,则暂停视频
// 如果页面是展示状态,则播放视频
function handleVisibilityChange() {
if (document[hidden]) {
videoElement.pause();
} else {
videoElement.play();
}
}
// 如果浏览器不支持 addEventListener 或 Page Visibility API 给出警告
if (
typeof document.addEventListener === "undefined" ||
typeof document[hidden] === "undefined"
) {
console.log(
"This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.",
);
} else {
// 处理页面可见属性的改变
document.addEventListener(visibilityChange, handleVisibilityChange, false);
// 当视频暂停,设置 title
// This shows the paused
videoElement.addEventListener(
"pause",
function () {
document.title = "Paused";
},
false,
);
// 当视频播放,设置 title
videoElement.addEventListener(
"play",
function () {
document.title = "Playing";
},
false,
);
}
属性
-
如果页面处于被认为是对用户隐藏状态时返回 true,否则返回 false。
Document.visibilityState
只读-
是一个用来展示文档当前的可见性的
DOMString
。该属性的值为以下值之一:visible
: 页面内容至少是部分可见。在实际中,这意味着页面是非最小化窗口的前景选项卡。hidden
: 页面内容对用户不可见。在实际中,这意味着文档可以是一个后台标签,或是最小化窗口的一部分,或是在操作系统锁屏激活的状态下。prerender
: 页面内容正在被预渲染且对用户是不可见的 (被 document.hidden 当做隐藏). 文档可能初始状态为 prerender,但绝不会从其他值转为该值。- 注释:有的浏览器不支持此功能
unloaded
: 页面正在从内存中卸载。 - 注释:有的浏览器不支持此功能
Document.onvisibilitychange
-
EventListener
提供在visibilitychange
事件被触发时要调用的代码。
js
//startSimulation 和 pauseSimulation 在其他地方定义
function handleVisibilityChange() {
if (document.hidden) {
pauseSimulation();
} else {
startSimulation();
}
}
document.addEventListener("visibilitychange", handleVisibilityChange, false);
规范
Specification |
---|
HTML Standard # dom-document-visibilitystate |
浏览器兼容性
Document.visibilityState
BCD tables only load in the browser
参考
- Description of the Page Visibility API on the IEBlog.
- Description of the Page Visibility API by Google