Created
February 11, 2014 23:50
-
-
Save pid/8946890 to your computer and use it in GitHub Desktop.
The browsers provide us with 2 key APIs which allow us to add Responsive Javascript to our site, they are the window.matchMedia API and the window.onresize API.
This file contains 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 resizeMethod = function(){ | |
if (document.body.clientWidth < 768) { | |
console.log('mobile'); | |
} | |
if (document.body.clientWidth > 768) { | |
console.log('desktop'); | |
} | |
}; | |
//Attach event for resizing | |
window.addEventListener("resize", resizeMethod, true); | |
//Prepare a MediaQueryList | |
var mql = window.matchMedia("(max-width:768px)"); | |
//Add a listener to the MediaQueryList | |
mql.addListener(function(e){ | |
if(e.matches){ | |
console.log('enter mobile'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment