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
|
---
title: MediaQueryList
slug: Web/API/MediaQueryList
tags:
- API
- CSSOM View
- Interface
- MediaQueryList
- NeedsTranslation
- Reference
- TopicStub
translation_of: Web/API/MediaQueryList
---
<div>{{APIRef("CSSOM View")}} {{SeeCompatTable}}</div>
<p>A <strong><code>MediaQueryList</code></strong> object stores information on a <a href="/en-US/docs/Web/CSS/Media_Queries">media query</a> applied to a document, and handles sending notifications to listeners when the media query state change (i.e. when the media query test starts or stops evaluating to <code>true</code>).</p>
<p>This makes it possible to observe a document to detect when its media queries change, instead of polling the values periodically, and allows you to programmatically make changes to a document based on media query status.</p>
<h2 id="Properties">Properties</h2>
<p><em>The new version of the <code>MediaQueryList</code> interface inherits properties from its parent interface, {{domxref("EventTarget")}}.</em></p>
<dl>
<dt>{{domxref("MediaQueryList.matches")}} {{readonlyInline}}</dt>
<dd>A {{domxref("Boolean")}} that returns <code>true</code> if the {{domxref("document")}} currently matches the media query list, or <code>false</code> if not.</dd>
<dt>{{domxref("MediaQueryList.media")}} {{readonlyInline}}</dt>
<dd>A {{domxref("DOMString")}} representing a serialized media query.</dd>
</dl>
<h3 id="Event_handlers">Event handlers</h3>
<dl>
<dt>{{domxref("MediaQueryList.onchange")}}</dt>
<dd>An event handler property representing a function that is invoked when the {{event("change")}} event fires, i.e when the status of media query support changes. The event object is a {{domxref("MediaQueryListEvent")}} instance, which is recognised as a <code>MediaListQuery</code> instance in older browsers, for backwards compatibility purposes.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<p><em>The new version of the <code>MediaQueryList</code> interface inherits methods from its parent interface, {{domxref("EventTarget")}}.</em></p>
<dl>
<dt>{{domxref("MediaQueryList.addListener()")}}</dt>
<dd>Adds a listener to the <code>MediaQueryListener</code> that will run a custom callback function in response to the media query status changing. This is basically an alias for {{domxref("EventTarget.addEventListener()")}}, for backwards compatibility purposes.</dd>
<dt>{{domxref("MediaQueryList.removeListener()")}}</dt>
<dd>Removes a listener from the <code>MediaQueryListener</code>. This is basically an alias for {{domxref("EventTarget.removeEventListener()")}}, for backwards compatibility purposes.</dd>
</dl>
<dl>
</dl>
<h2 id="Examples">Examples</h2>
<p>This simple example creates a <code>MediaQueryList</code> and then sets up a listener to detect when the media query status changes, running a custom function when it does to change the appearence of the page.</p>
<pre class="brush: js">var para = document.querySelector('p');
var mql = window.matchMedia('(max-width: 600px)');
function screenTest(e) {
if (e.matches) {
/* the viewport is 600 pixels wide or less */
para.textContent = 'This is a narrow screen — less than 600px wide.';
document.body.style.backgroundColor = 'red';
} else {
/* the viewport is more than than 600 pixels wide */
para.textContent = 'This is a wide screen — more than 600px wide.';
document.body.style.backgroundColor = 'blue';
}
}
mql.addListener(screenTest);</pre>
<div class="note">
<p><strong>Note</strong>: You can find this example on GitHub (see the <a href="https://github.com/mdn/dom-examples/blob/master/mediaquerylist/index.html">source code</a>, and also see it <a href="https://mdn.github.io/dom-examples/mediaquerylist/index.html">running live</a>).</p>
</div>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th>Specification</th>
<th>Status</th>
<th>Comment</th>
</tr>
<tr>
<td>{{SpecName("CSSOM View", "#the-mediaquerylist-interface", "MediaQueryList")}}</td>
<td>{{Spec2("CSSOM View")}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.MediaQueryList")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/CSS/Media_queries">Media queries</a></li>
<li><a href="/en-US/docs/CSS/Using_media_queries_from_code">Using media queries from code</a></li>
<li>{{domxref("window.matchMedia()")}}</li>
<li>{{domxref("MediaQueryListListener")}}</li>
<li>{{domxref("MediaQueryListEvent")}}</li>
</ul>
|