Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Orientation Change
/*
Device agnostic orientation check
=================================
Should work on most mobile platforms. Trying changing the 250ms timeout to a
higher value if still doesn't work on some devices.
Can't rely on window.orientation as wrong result given on some Samsung devices.
iOS doesn't need the 250ms timeout and reports the correct result for
window.orientation, so just use that if only targetting iOS devices.
Assumes jQuery is being used, but you could swap that code for slightly
more verbose window height/width lookups.
*/
orientationChange = function () {
var change = function () {
setTimeout(function () {
var height = window.innerHeight || $(window).height();
var width = window.innerWidth || $(window).width();
if (width > height) {
// Landscape functions
} else {
// Portrait functions
}
}, 250);
},
orientationEvent = (window.onorientationchange) ? "orientationchange" : "resize";
window.addEventListener(orientationEvent, change, false);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.