Document.visibilityState - Web APIs 编辑

The Document.visibilityState read-only property returns the visibility of the document, that is in which context this element is now visible. It is useful to know if the document is in the background or an invisible tab, or only loaded for pre-rendering. Possible values are:

visible
The page content may be at least partially visible. In practice this means that the page is the foreground tab of a non-minimized window.
hidden
The page content is not visible to the user. In practice this means that the document is either a background tab or part of a minimized window, or the OS screen lock is active.
Safari doesn’t fire the visibilitychange event as expected when visibilityState transitions to hidden; so for that case, you need to also include code to listen for the pagehide event.
prerender This deprecated API should no longer be used, but will probably still work.
The page content is being prerendered and is not visible to the user (considered hidden for purposes of document.hidden). The document may start in this state, but will never transition to it from another value. Note: This was removed from the standard. Check compatibility table for details.

When the value of this property changes, the visibilitychange event is sent to the Document.

Typical use of this can be to prevent the download of some assets when the document is solely prerendered, or stop some activities when the document is in the background or minimized.

Syntax

var string = document.visibilityState

Examples

document.addEventListener("visibilitychange", function() {
  console.log( document.visibilityState );
  // Modify behavior...
})

Specifications

SpecificationStatusComment
Page Visibility (Second Edition)
The definition of 'Document.visibilityState' in that specification.
RecommendationInitial definition.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:55 次

字数:4175

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文