Skip to content

Instantly share code, notes, and snippets.

@pid
Created February 11, 2014 23:50
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 pid/8946890 to your computer and use it in GitHub Desktop.
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.
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