Skip to content

Instantly share code, notes, and snippets.

@marioluevanos
Created January 22, 2020 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marioluevanos/d3382c5898a1adb8d980ed5910b1786d to your computer and use it in GitHub Desktop.
Save marioluevanos/d3382c5898a1adb8d980ed5910b1786d to your computer and use it in GitHub Desktop.
Media Query List Web API example
var para = document.querySelector('p');
var mql = window.matchMedia('(max-width: 600px)');
function screenTest(e) {
if (e.matches) {
/* the viewport is 600 pixels wide or less */
para.textContent = 'This is a narrow screen — less than 600px wide.';
document.body.style.backgroundColor = 'red';
} else {
/* the viewport is more than than 600 pixels wide */
para.textContent = 'This is a wide screen — more than 600px wide.';
document.body.style.backgroundColor = 'blue';
}
}
screenTest(mql);
mql.addListener(screenTest);
mql.onchange = function() {
console.log(mql);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment