Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  77] [ 7]  / answers: 1 / hits: 16705  / 12 Years ago, mon, december 24, 2012, 12:00:00

I am testing my Web Application on iPad (Safari browser) and Samsung Tab 2 (Default browser). The window.orientationchange returns different values in both the devices.



$(document).ready(function() {
window.addEventListener(orientationchange, centerLoginBox);
window.addEventListener(load, centerLoginBox);
});

function centerLoginBox() {
if (window.orientation == 90 || window.orientation == -90) { //Landscape Mode
$('#loginbox').css('margin-top', '20%');
alert(window.orientation);
}
else if (window.orientation == 0 || window.orientation == 180) { //Portrait Mode
$('#loginbox').css('margin-top', '40%');
alert(window.orientation);
}


In Tab 2 the alert throws '0' and '180' for landscape mode and the values '90' and '-90' for portrait mode(just the opposite behavior in iPad).



Is this some kind of design difference in iOS and Android? What could be a common solution for this issue?


More From » android

 Answers
21

Ok, this is what I did. I queried for the User Agent information and checked if the device was Android based. If so, change the conditions of window.orientation for both Portrait and Landscape mode.



CODE



function centerLoginBox() {
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf(android) > -1; // Detect Android devices
if (isAndroid) {
//window.orientation is different for iOS and Android
if (window.orientation == 0 || window.orientation == 180) { //Landscape Mode
$('#loginbox').css('margin-top', '20%');
}
else if (window.orientation == 90 || window.orientation == -90) { //Portrait Mode
$('#loginbox').css('margin-top', '40%');
}
}
else {
if (window.orientation == 90 || window.orientation == -90) { //Landscape Mode
$('#loginbox').css('margin-top', '20%');
}
else if (window.orientation == 0 || window.orientation == 180) { //Portrait Mode
$('#loginbox').css('margin-top', '40%');
}
}
}

[#81246] Saturday, December 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jennie

Total Points: 593
Total Questions: 102
Total Answers: 106

Location: Federated States of Micronesia
Member since Fri, Sep 16, 2022
2 Years ago
jennie questions
;