Let's say I have multiple CSS style sheets which apply at different resolutions with media queries, how do you detect which CSS stylesheets are currently active and being used with JavaScript OR jQuery?
Thanks :)
Let's say I have multiple CSS style sheets which apply at different resolutions with media queries, how do you detect which CSS stylesheets are currently active and being used with JavaScript OR jQuery?
Thanks :)
To get the current @media in effect, you can use the new MediaQueryList object in window. It has matchMedia method that you can pass media query like this.
if (window.matchMedia("(min-width: 400px)").matches) {
/* the viewport is at least 400 pixels wide */
} else {
/* the viewport is less than 400 pixels wide */
}
You can register a listener instead of poll the query's matchMedia result. This can be done by calling addListener() method like this:
var mql = window.matchMedia("(min-width: 400px)");
mql.addListener(MediaQueryChange);
MediaQueryChange(mql);
// media query has changed
function MediaQueryChange(mq) {
if (mq.matches) {
/* the viewport is at least 400 pixels wide */
}
else {
/* the viewport is less than 400 pixels wide */
}
}