diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/document/visibilitychange_event | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/document/visibilitychange_event')
-rw-r--r-- | files/zh-cn/web/api/document/visibilitychange_event/index.html | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/document/visibilitychange_event/index.html b/files/zh-cn/web/api/document/visibilitychange_event/index.html new file mode 100644 index 0000000000..f1c945c5ea --- /dev/null +++ b/files/zh-cn/web/api/document/visibilitychange_event/index.html @@ -0,0 +1,124 @@ +--- +title: visibilitychange +slug: Web/API/Document/visibilitychange_event +tags: + - API + - Visibility + - visibilitychange +translation_of: Web/API/Document/visibilitychange_event +--- +<p>{{APIRef}}</p> + +<p>当其选项卡的内容变得可见或被隐藏时,会在文档上触发 <code>visibilitychange</code> (能见度更改)事件。</p> + +<h2 id="概述">概述</h2> + +<dl> + <dt style="float: left; text-align: right; width: 120px;">Specification</dt> + <dd style="margin: 0pt 0pt 0pt 120px;">{{SpecName("Page Visibility API")}}</dd> + <dt style="float: left; text-align: right; width: 120px;">Interface</dt> + <dd style="margin: 0pt 0pt 0pt 120px;">{{domxref("event")}}</dd> + <dt style="float: left; text-align: right; width: 120px;">Bubbles</dt> + <dd style="margin: 0pt 0pt 0pt 120px;">Yes</dd> + <dt style="float: left; text-align: right; width: 120px;">Cancelable</dt> + <dd style="margin: 0pt 0pt 0pt 120px;">No</dd> + <dt style="float: left; text-align: right; width: 120px;">Target</dt> + <dd style="margin: 0pt 0pt 0pt 120px;">{{domxref("Document")}}</dd> + <dt style="float: left; text-align: right; width: 120px;">Default Action</dt> + <dd style="margin: 0pt 0pt 0pt 120px;">None</dd> +</dl> + +<h2 id="使用说明">使用说明</h2> + +<p>该事件不包括文档的更新的可见性状态,但是您可以从文档的 {{domxref("Document.visibilityState", "visibilityState")}} 属性中获取该信息。</p> + +<div class="blockIndicator warning"> +<p>当 visibleStateState 属性的值转换为 <code>hidden </code>时,Safari不会按预期触发<code>visibilitychange</code>; 因此,在这种情况下,您还需要包含代码以侦听 <code><a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event">pagehide</a></code> 事件。</p> +</div> + +<div class="blockIndicator warning"> +<p>出于兼容性原因,请确保使用 <code>document.addEventListener </code>而不是<code>window.addEventListener</code>来注册回调。 Safari <14.0仅支持前者。</p> +</div> + +<h2 id="属性">属性</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Property</th> + <th scope="col">Type</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>target</code> {{readonlyInline}}</td> + <td>{{domxref("EventTarget")}}</td> + <td>The event target (the topmost target in the DOM tree).</td> + </tr> + <tr> + <td><code>type</code> {{readonlyInline}}</td> + <td>{{domxref("DOMString")}}</td> + <td>The type of event.</td> + </tr> + <tr> + <td><code>bubbles</code> {{readonlyInline}}</td> + <td>{{jsxref("Boolean")}}</td> + <td>Whether the event normally bubbles or not.</td> + </tr> + <tr> + <td><code>cancelable</code> {{readonlyInline}}</td> + <td>{{jsxref("Boolean")}}</td> + <td>Whether the event is cancellable or not.</td> + </tr> + </tbody> +</table> + +<h2 id="例子">例子</h2> + +<p>本示例在文档可见时开始播放音乐曲目,在文档不再可见时暂停音乐。</p> + +<pre class="brush:js; notranslate">document.addEventListener("visibilitychange", function() { + console.log( document.visibilityState ); +}); +</pre> + +<pre class="notranslate">document.addEventListener("visibilitychange", function() { + if (document.visibilityState === 'visible') { + backgroundMusic.play(); + } else { + backgroundMusic.pause(); + } +});</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + </thead> + <tbody> + <tr> + <td><a class="external" href="http://dvcs.w3.org/hg/webperf/raw-file/tip/specs/PageVisibility/Overview.html" title="http://dvcs.w3.org/hg/webperf/raw-file/tip/specs/PageVisibility/Overview.html">W3C Page Visibility API</a></td> + <td>{{Spec2('Page Visibility API')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<p>{{Compat("api.Document.visibilitychange")}}</p> + +<p>{{Compat("api.Document.visibilitychange_event")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li><a href="/zh-CN/docs/DOM/Using_the_Page_Visibility_API" title="DOM/Using_the_Page_Visibility_API">使用页面可见性API</a></li> + <li>{{domxref("Document.visibilityState")}}</li> +</ul> |