diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
| commit | 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch) | |
| tree | a9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/uk/web/api/mediaquerylist/index.html | |
| parent | 074785cea106179cb3305637055ab0a009ca74f2 (diff) | |
| download | translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2 translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip | |
initial commit
Diffstat (limited to 'files/uk/web/api/mediaquerylist/index.html')
| -rw-r--r-- | files/uk/web/api/mediaquerylist/index.html | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/files/uk/web/api/mediaquerylist/index.html b/files/uk/web/api/mediaquerylist/index.html new file mode 100644 index 0000000000..7e22ce652e --- /dev/null +++ b/files/uk/web/api/mediaquerylist/index.html @@ -0,0 +1,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> |
