[Javascript] Window.matchMedia()

window.matchMedia() allow to listen to browser window size changes and trigger the callback for different media query size.

let mql = window.matchMedia('(max-width: 600px)');

 

 

you can attach a listener to it:

mql.addListener(() => console.log('changes'));

So when the window size is smaller than 600px, it matches is true otherwise is false.

When you resize the window, it will log out "changes" in console.

 

posted @ 2019-11-05 17:10  Zhentiw  阅读(328)  评论(0)    收藏  举报