Skip to content

Instantly share code, notes, and snippets.

@alehano
Last active August 29, 2015 14:00
Show Gist options
  • Save alehano/11302364 to your computer and use it in GitHub Desktop.
Save alehano/11302364 to your computer and use it in GitHub Desktop.
Responsive CSS
/*
Depend of screen size it adds to all element with class "scr", additional class (e.g. "scr-sm").
In a css file you can define representation of elements for different devices.
CSS e.g.
element.scr-xs {
... style for mobile phones
}
*/
function viewport() {
var e = window, a = 'inner';
if ( !( 'innerWidth' in window ) ) {
a = 'client';
e = document.documentElement || document.body;
}
return { width : e[ a+'Width' ], height : e[ a+'Height' ] }
}
function responsiveCss() {
var wd = viewport().width;
var cl = "scr-md";
if (wd <=768) {
cl = "scr-xs"; // mobile
} else if (wd >= 768 && wd < 992) {
cl = "scr-sm"; // tablet
} else if (wd >= 1200) {
cl = "scr-lg";
}
$(".scr").each(function(){
$(this).removeClass("scr-xs scr-sm scr-md scr-lg");
$(this).addClass(cl);
});
}
$(document).ready(function() {
responsiveCss();
window.onresize = function() {
responsiveCss();
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment