A
A
Azat S.2016-04-08 19:39:27
JavaScript
Azat S., 2016-04-08 19:39:27

MatchMedia not working when browser window is resized?

If you resize the browser window and refresh the page, everything works, but the listener does not work when the window is resized.
codepen.io/azat-io/pen/oxogRJ

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2016-04-08
@Deonisius

Short abstract example

var mediaQuery = window.matchMedia("screen and (min-width: 855px)");
mediaQuery.addListener(myFunc);

function myFunc(mq) {
    if (mq.matches) {
    	document.body.style.backgroundColor = '#060';
    } else {
    document.body.style.backgroundColor = '#900';
    }
}
myFunc(mediaQuery);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question