Is there a javascript equivalent to using @media query? [duplicate]

You can use window.matchMedia() for media queries in javascript.

for example

var mq = window.matchMedia( "(max-width: 570px)" );
if (mq.matches) {
    // window width is at less than 570px
}
else {
    // window width is greater than 570px
}

Please refer the below links for better understanding

https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
http://www.sitepoint.com/javascript-media-queries/

Updated as per comment

Please refer the plunker: “http://plnkr.co/edit/wAKFEFz0NU6ZaEmywlgc?p=preview

For better understanding: “http://www.javascriptkit.com/javatutors/matchmediamultiple.shtml

For web browser support information:
https://hacks.mozilla.org/2012/06/using-window-matchmedia-to-do-media-queries-in-javascript/

Leave a Comment