blob: f1c945c5ea405c8da77eb0cbc9388ab7402819f2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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>
|