MDN wants to talk to developers like you: https://qsurvey.mozilla.com/s3/8d22564490d8

Document.visibilityState

这篇翻译不完整。请帮忙从英语翻译这篇文章

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.
  • 'prerender' : 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: browser support is optional.
  • 'unloaded' : the page is being unloaded from memory. Note: browser support is optional.

When the value of this property change, 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. Note that browser.

语法

var string = document.visibilityState

例子

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

特殊情况

Specification Status Comment
Page Visibility (Second Edition)
Document.visibilityState
Recommendation Initial definition.

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 13 webkit
33
18 (18) [2] 10 12.10[1] 7
prerender (Yes) 49 (49) ? ? ?
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 4.4 webkit 18.0 (18) [2] ? 12.10[1] 7
prerender (Yes) 49.0 (49) ? ??

[1] Doesn't fire the visibilitychange event when the browser window is minimized, nor set hidden to true.

[2] From Firefox 10 to Firefox 51 included, this property could be used prefixed with -moz-.

文档标签和贡献者

 此页面的贡献者: printfalling
 最后编辑者: printfalling,