r/javascript • • Aug 21 '18

Effectively supported in all browsers, JavaScript has its own form of media queries just like CSS.

https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
465 Upvotes

114 comments sorted by

View all comments

0

u/[deleted] Aug 22 '18

[deleted]

8

u/xraminator Aug 22 '18 edited Aug 22 '18

Media queries can be used for much more than window width.

With regard to it being an event, it is. window.matchMedia returns a MediaQueryList object that includes addListener and removeListener methods. The listeners are fired when the matches state of the media query changes. As your listener is only invoked when the relevant threshold is crossed this technique is much more efficient than using a resize event which will fire constantly during resizing.

https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/addListener#Examples

EDIT: Typo.

4

u/_www_ Aug 22 '18

My eyes are bleeding when I see typos in function names.

2

u/xraminator Aug 22 '18

It's not a function :-P