Skip to content

Instantly share code, notes, and snippets.

@davidicus
Created January 10, 2013 17:36
Show Gist options
  • Save davidicus/4504081 to your computer and use it in GitHub Desktop.
Save davidicus/4504081 to your computer and use it in GitHub Desktop.
new API for adding an event listener and testing for media queries.
var mq = window.matchMedia("(min-width: 480px)");
if (mq.matches) {
// window width is at least 480px
$('.slideOne').attr('src', 'img/slideOne.jpg');
$('.slideTwo').attr('src', 'img/slideTwo.jpg');
$('.slideThree').attr('src', 'img/slideThree.jpg');
$('.slideFour').attr('src', 'img/slideFour.jpg');
$('.slideFive').attr('src', 'img/slideFive.jpg');
$('.slideSix').attr('src', 'img/slideSix.jpg');
}
// media query event handler
if (matchMedia) {
var mq = window.matchMedia("(min-width: 480px)");
//mq.addListener(WidthChange);
WidthChange(mq);
}
// media query change
function WidthChange(mq) {
if (mq.matches) {
// window width is at least 480px. do this code
}
else {
// window width is less than 480px. do this code
}
}
@davidicus
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment