MediaQueryListEvent

MediaQueryListEvent object stores information on the changes that have happened to a MediaQueryList object — instances are available as the event object on a function referenced by a MediaQueryList.onchange property or MediaQueryList.addListener() 调用。

构造函数

MediaQueryListEvent()
创建新的 MediaQueryListEvent 实例。

特性

MediaQueryListEvent interface inherits properties from its parent interface, Event .

MediaQueryListEvent.matches 只读
A Boolean that returns true document currently matches the media query list, or false if not.
MediaQueryListEvent.media 只读
A DOMString representing a serialized media query.

方法

MediaQueryListEvent interface inherits methods from its parent interface, Event .

范例

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);
					

规范

规范 状态 Comment
CSS Object Model (CSSOM) View Module
The definition of 'MediaQueryListEvent' in that specification.
工作草案 初始定义

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
MediaQueryListEvent Chrome Yes Edge ≤79 Firefox 55 IE No Opera Yes Safari ? WebView Android No Chrome Android Yes Firefox Android 55 Opera Android Yes Safari iOS ? Samsung Internet Android Yes
MediaQueryListEvent() 构造函数 Chrome Yes Edge ≤79 Firefox 55 IE No Opera Yes Safari ? WebView Android No Chrome Android Yes Firefox Android 55 Opera Android Yes Safari iOS ? Samsung Internet Android Yes
matches Chrome Yes Edge ≤79 Firefox 55 IE No Opera Yes Safari ? WebView Android No Chrome Android Yes Firefox Android 55 Opera Android Yes Safari iOS ? Samsung Internet Android Yes
media Chrome Yes Edge ≤79 Firefox 55 IE No Opera Yes Safari ? WebView Android No Chrome Android Yes Firefox Android 55 Opera Android Yes Safari iOS ? Samsung Internet Android Yes

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知

另请参阅