一般检查任何移动设备屏幕方向更改?

问题描述 投票:0回答:1

我有一个网站可以根据移动设备的方向是纵向还是横向来改变显示的内容。我目前发现的是我有时会使用的当前代码,但在随机设备上,它将不会按预期工作。设备的方向将被翻转,因此我想在potrait上显示的页面在横向上显示,反之亦然。以下是我正在使用的代码适用于所有Android设备,但可以在某些iOS设备上运行而不在其他设备上运行。

// determines device type for orientation
                var isMobile = {
                        Android: function() {
                                return navigator.userAgent.match(/Android/i);
                        },
                        BlackBerry: function() {
                                return navigator.userAgent.match(/BlackBerry/i);
                        },
                        iOS: function() {
                                return navigator.userAgent.match(/iPhone|iPad|iPod/i);
                        },
                        Opera: function() {
                                return navigator.userAgent.match(/Opera Mini/i);
                        },
                        Windows: function() {
                                return navigator.userAgent.match(/IEMobile/i);
                        },
                        any: function() {
                                return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
                        }
                };


 window.addEventListener("orientationchange", function() {
        // if it is an iPhone, swap screen orientation doing this
                 if(isMobile.iOS()){
                        if (window.matchMedia("(orientation: portrait)").matches) {
                                // you're in PORTRAIT mode
                potraitBody.classList.add("hideThisDiv");
                                landscapeBody.classList.remove("hideThisDiv");
                        }

                        if (window.matchMedia("(orientation: landscape)").matches) {
                                // you're in LANDSCAPE mode
                potraitBody.classList.remove("hideThisDiv");
                                landscapeBody.classList.add("hideThisDiv");
                        }
                }
                // if it is on Android swap screen orientation doing this
                if(isMobile.Android()) {
                        if(screen.orientation.angle == 0)
                        {
                                // portrait
                potraitBody.classList.remove("hideThisDiv");
                                landscapeBody.classList.add("hideThisDiv");
                        }
                        else
                        {
                                // landscape
                potraitBody.classList.add("hideThisDiv");
                                landscapeBody.classList.remove("hideThisDiv");
                        }
                }
                }, false);

我解决这个问题的第一个解决方案是使用监听器进行oreintationchange,然后简单地检查方向是0(纵向)还是0(横向)。这适用于某些iOS设备和所有Android设备。然后我添加了针对iOS设备的特定检查,认为它们都以相同的方式定向,但是当检查iPhone 10时,我的代码可以工作,但是在iPhone 7或iPad Pro 2017型号上,它没有。是否有解决方案在所有移动设备上正确检查方向?我真的不想追捕每个设备可能会使用我的网站来明确地适应它。

谢谢!

编辑:

如果我的问题不够具体,我正在寻找一种方法来测试任何移动设备是处于横向模式还是potrait模式。如果可能的话,我不想通过模型逐个制作案例,但操作系统的操作系统很好。我认为有一种通用的方法来检查移动设备上的屏幕方向。

javascript html ios mobile screen-orientation
1个回答
0
投票

如果其他人有这个问题,我发现这个解决方案对我有用。

var handleOrientationChange = (function() {
    var struct = function(){
        struct.parse();
    };
    struct.showPortraitView = function(){
         potraitBody.classList.remove("hideThisDiv");
         landscapeBody.classList.add("hideThisDiv");
    };
    struct.showLandscapeView = function(){
         potraitBody.classList.add("hideThisDiv");
         landscapeBody.classList.remove("hideThisDiv"); 
    };
    struct.parse = function(){
        switch(window.orientation){
            case 0:
                    //Portrait Orientation
                    this.showPortraitView();
                break;
            default:
                  // Landscape Orientation
                   this.lastOrientation = window.orientation;
                   this.showLandscapeView();
                break;
        }
    };
    struct.lastOrientation = window.orientation;
    return struct;
})();

在iPad,Galaxy s9 +,iPhone 7,iPhone 10和iPhone SE上工作。没有在其他设备上测试它,但我相信它应该可以在任何地方使用。

© www.soinside.com 2019 - 2024. All rights reserved.