Skip to content

Instantly share code, notes, and snippets.

@st3phan
Forked from bergantine/gist:1445036
Created December 7, 2011 23:02
Show Gist options
  • Save st3phan/1445179 to your computer and use it in GitHub Desktop.
Save st3phan/1445179 to your computer and use it in GitHub Desktop.
Responsive JS
window.onresize = function() {
responsiveLoad();
}
window.onload = function() {
responsiveLoad();
}
function responsiveLoad() {
console.log('resized');
if (matchMedia) {
var mqs = window.matchMedia("(min-width: 951px)");
mqs.addListener(WidthChangeToScreen);
WidthChangeToScreen(mqs);
var mqt = window.matchMedia("(min-width: 761px) and (max-width: 950px)");
mqt.addListener(WidthChangeToTablet);
WidthChangeToTablet(mqt);
var mqm = window.matchMedia("(max-width: 760px)");
mqm.addListener(WidthChangeToMobile);
WidthChangeToMobile(mqm);
}
}
function WidthChangeToScreen(mq) {
if (mq.matches) {
// screen is >= 951px wide
}
}
function WidthChangeToTablet(mq) {
if (mq.matches) {
// screen is >= 761px && <= 950px wide
}
}
function WidthChangeToMobile(mq) {
if (mq.matches) {
// screen is <= 760px wide
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment