blob: 00d72d378fc6e3c2900ddff763f8e90a90c0b23e (
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
|
---
title: 測試 media queries
slug: Web/CSS/Media_Queries/Testing_media_queries
translation_of: Web/CSS/Media_Queries/Testing_media_queries
---
<p>{{SeeCompatTable}}</p>
<p>DOM 提供了一個用程式去測試 media query 的方法:那就是透過 {{domxref("MediaQueryList") }} 物件;透過 <span style="line-height: 19.0909080505371px;">{{domxref("MediaQueryList") }} 物件上的屬性和方法,我們可以得知、觀察目前 media query 的狀態。</span></p>
<h2 id="Creating_a_media_query_list" name="Creating_a_media_query_list">創建 media query list 物件</h2>
<p>透過傳入目標 medai query 到 {{domxref("window.matchMedia") }} 方法,我們將可以取得相對應的 <span style="line-height: 19.0909080505371px;">MediaQueryList 物件。</span></p>
<p>以下範例將取得偵測螢幕方向的 <span style="line-height: 19.0909080505371px;">MediaQueryList 物件:</span></p>
<pre>var mql = window.matchMedia("(orientation: portrait)");
</pre>
<h2 id="Checking_the_result_of_a_query" name="Checking_the_result_of_a_query">檢查 media query 結果</h2>
<p>讀取 <span style="line-height: 19.0909080505371px;">MediaQueryList 物件 的 matches 屬性就可以得知測試結果:</span></p>
<pre class="brush: js">if (mql.matches) {
/* The device is currently in portrait orientation */
} else {
/* The device is currently in landscape orientation */
}
</pre>
<h2 id="Receiving_query_notifications" name="Receiving_query_notifications">接收測試結果通知</h2>
<p>當我們想要動態依據 media query 測試狀況執行對應處理,除了持續定期偵測外,還有一個更有效率的途徑:那就是註冊 media query 的事件處理器 (listener)。只要呼叫 <span style="line-height: 19.0909080505371px;">MediaQueryList 物件上 addListener 方法,傳入符合</span> {{domxref("MediaQueryListListener") }} 介面的 listener 便完成事件註冊<span style="line-height: 19.0909080505371px;">:</span></p>
<pre class="brush: js">var mql = window.matchMedia("(orientation: portrait)");
mql.addListener(handleOrientationChange);
handleOrientationChange(mql);
</pre>
<p>上面我們替螢幕方向 media query 註冊了一個 listener。請注意,在第一次註冊後,listener 會立刻被呼叫一次,這是為了初始化準備,因為我們可能預設 UI 是針對 'portait' 模式,但目前卻是 'landscape' 模式,所以這個第一次呼叫將給予我們作初始化調整的空間。</p>
<p><code>下面handleOrientationChange()</code> 方法便是我們針對螢幕方向變化的 listener<span style="line-height: 19.0909080505371px;">:</span></p>
<pre class="brush: js">function handleOrientationChange(mql) {
if (mql.matches) {
/* The device is currently in portrait orientation */
} else {
/* The device is currently in landscape orientation */
}
}
</pre>
<h2 id="Ending_query_notifications" name="Ending_query_notifications">停止接收測試結果通知</h2>
<p>當我們不需要接受通知時,只需要呼叫 removeListener 方法,然後傳入欲移除的 listener 即可:</p>
<pre>mql.removeListener(handleOrientationChange);
</pre>
<h2 id="Browser_compatibility" name="Browser_compatibility">瀏覽器相容性</h2>
{{Compat("api.MediaQueryList")}}
<h2 id="See_also" name="See_also"> </h2>
<h2 id="See_also" name="See_also">延伸閱讀</h2>
<ul>
<li><a href="/en-US/docs/CSS/Media_queries" title="CSS/Media queries">Media queries</a></li>
<li>{{domxref("window.matchMedia()") }}</li>
<li>{{domxref("MediaQueryList") }}</li>
<li>{{domxref("MediaQueryListListener") }}</li>
</ul>
|