Created
January 22, 2020 03:35
-
-
Save marioluevanos/d3382c5898a1adb8d980ed5910b1786d to your computer and use it in GitHub Desktop.
Media Query List Web API example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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