Skip to content

Instantly share code, notes, and snippets.

@johnboxall
Created May 3, 2010 01:56
Show Gist options
  • Save johnboxall/387650 to your computer and use it in GitHub Desktop.
Save johnboxall/387650 to your computer and use it in GitHub Desktop.
// Add orientation body class.
window.addEventListener("DOMContentLoaded", function(){
var cls = ["landscape", "portrait"];
var clsRe = new RegExp("(" + cls.join("|") + ")");
var event = "onorientationchange" in window ? "orientationchange" : "resize";
document.body.className += " " + getClass();
window.addEventListener(event, function(){
var className = document.body.className;
document.body.className = className ? className.replace(clsRe, getClass()) : getClass();
}, true);
function getClass(){
return cls[window.orientation % 180 ? 0 : 1];
}
}, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment