Skip to content

Instantly share code, notes, and snippets.

@garnu
Created May 5, 2013 18:34
Show Gist options
  • Save garnu/5521705 to your computer and use it in GitHub Desktop.
Save garnu/5521705 to your computer and use it in GitHub Desktop.
Detect orientation change on iOS and Android + 180 degrees rotation fix for Android.
var orientation = (window.innerHeight > window.innerWidth)? "portrait" : "landscape";
$(window).on("orientationchange resize", checkOrientation);
var checkOrientation = function() {
var newOrientation = (window.innerHeight > window.innerWidth)? "portrait" : "landscape";
if(newOrientation == orientation) { return; } // no change
setInterval(checkOrientation, 1000); // recheck after 1 second for Android devices
orientation = newOrientation;
if(orientation == "portrait") {
// do something
}
else { // portrait
// do something else
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment