window.matchMedia
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
概要
指定された メディアクエリ文字列のパース結果を表す、新しい MediaQueryList
オブジェクトを返します。
構文
mql = window.matchMedia(mediaQueryString)
mediaQueryString
は、新しい MediaQueryList
オブジェクトを返すための、メディアクエリ文字列です。
例
js
if (window.matchMedia("(min-width: 400px)").matches) {
/* ビューポートの幅が 400 ピクセル以上の場合のコードをここに */
} else {
/* ビューポートの幅は 400 ピクセル未満の場合のコードをここに */
}
このコードでは、ウィンドウが非常に狭いときに、何か別の処理を行うことができます。
他にも『コード内でのメディアクエリの使用』にいくつか例があります。
仕様
Specification |
---|
CSSOM View Module # dom-window-matchmedia |
ブラウザ互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
matchMedia |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.