window.orientation返回在iOS和Android不同的值 [英] window.orientation returns different values in iOS and Android

查看:344
本文介绍了window.orientation返回在iOS和Android不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的iPad (Safari浏览器)的测试我的Web应用程序和三星标签2 (默认浏览器)。所述的 window.orientationchange 的同时在设备返回的值不同。

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);
            }

在标签2警报将引发'0'和'180'的风景的模式和值90和-90对人像的模式(<强>就在iPad上的相反的行为)。

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).

这是在iOS和Android某种设计的区别?可能是什么这个问题的共同解决方案?

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

推荐答案

好吧,这是我做的。我查询了用户代理信息,如果是基于Android的设备检查。如果是的话,改变的条件的 window.orientation 的两个人像和风景模式。

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%');
            }
        }
    }

这篇关于window.orientation返回在iOS和Android不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆