-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
68 lines (47 loc) · 1.83 KB
/
index.md
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
---
title: "MediaQueryList: change event"
short-title: change
slug: Web/API/MediaQueryList/change_event
page-type: web-api-event
browser-compat: api.MediaQueryList.change_event
---
{{APIRef("CSSOM")}}
The **`change`** event of the {{DOMxRef("MediaQueryList")}} interface fires when the status of media query support changes.
## Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
```js
addEventListener("change", (event) => {});
onchange = (event) => {};
```
## Event type
A {{domxref("MediaQueryListEvent")}}. Inherits from {{domxref("Event")}}.
{{InheritanceDiagram("MediaQueryListEvent")}}
## Event properties
_The `MediaQueryListEvent` interface inherits properties from its parent interface, {{DOMxRef("Event")}}._
- {{DOMxRef("MediaQueryListEvent.matches")}} {{ReadOnlyInline}}
- : A boolean value that is `true` if the {{DOMxRef("document")}} currently matches the media query list, or `false` if not.
- {{DOMxRef("MediaQueryListEvent.media")}} {{ReadOnlyInline}}
- : A string representing a serialized media query.
## Example
```js
const mql = window.matchMedia("(max-width: 600px)");
mql.onchange = (e) => {
if (e.matches) {
/* the viewport is 600 pixels wide or less */
console.log("This is a narrow screen — less than 600px wide.");
} else {
/* the viewport is more than 600 pixels wide */
console.log("This is a wide screen — more than 600px wide.");
}
};
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Media queries](/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries)
- [Using media queries from code](/en-US/docs/Web/CSS/CSS_media_queries/Testing_media_queries)
- {{DOMxRef("window.matchMedia()")}}
- {{DOMxRef("MediaQueryList")}}
- {{DOMxRef("MediaQueryListEvent")}}