简体   繁体   English

一般检查移动设备的屏幕方向是否有变化?

[英]General check for any mobile device screen orientation change?

I have a website that will change what is shown depending on if the mobile device's orientation is either portrait or landscape. 我有一个网站,该网站会根据移动设备的方向是纵向还是横向来更改显示的内容。 What I am currently finding is the current code I have works sometimes, but on random devices, it will not work as intended. 我目前发现的是我有时可以使用的当前代码,但是在随机设备上,它将无法按预期运行。 The orientation of the device will be flipped, so the page I want to show on potrait is shown on landscape and vice versa. 设备的方向将翻转,因此我要在potrait上显示的页面在横向显示,反之亦然。 Here is the code I am using that works on all Android devices, but will work on some iOS devices while not working on others. 这是我正在使用的代码,该代码可在所有Android设备上使用,但可在某些iOS设备上使用,而不能在其他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);

My first solution to solving this issue was using the listener for oreintationchange, then simply checking if the orientation was 0 (portrait) or not 0 (landscape). 解决此问题的第一个解决方案是将监听器用于oreintationchange,然后简单地检查方向是否为0(纵向)或不为0(横向)。 This work on some iOS devices and all Android devices. 在某些iOS设备和所有Android设备上均可使用。 I then added the specific check for iOS devices thinking that they all were oriented the same way, but when checking on an iPhone 10, my code works, but on an iPhone 7 or iPad Pro 2017 model, it does not. 然后,我添加了针对iOS设备的特定检查,认为它们都以相同的方式定向,但是在iPhone 10上进行检查时,我的代码有效,但是在iPhone 7或iPad Pro 2017型号上却没有。 Is there a solution to check orientation correctly on all mobile devices? 是否有解决方案可在所有移动设备上正确检查方向? I do not really want to hunt down every device might use my site to accomidate for it specfically. 我并不是真的想搜寻所有可能会使用我的网站来专门针对它的设备。

Thanks! 谢谢!

Edit: 编辑:

If my question is not specific enough, I am looking for a method to test if any mobile device is in either landscape or potrait mode. 如果我的问题不够具体,我正在寻找一种方法来测试任何移动设备是否处于横向或纵向模式。 If possible I would not like to go through and make cases model by model, but OS by OS is fine. 如果可能的话,我不希望逐个模型地进行案例分析,但是逐个操作系统是可以的。 I would think there is a universal way to check screen orientation on mobile. 我认为有一种通用的方法可以检查手机上的屏幕方向。

Here is the solution I found that worked for me if anyone else has this question. 如果有人遇到这个问题,这是我发现对我有用的解决方案。

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

Worked on an iPad, Galaxy s9+, iPhone 7, iPhone 10 and iPhone SE. 适用于iPad,Galaxy s9 +,iPhone 7,iPhone 10和iPhone SE。 Haven't tested it on other devices but I believe it should work everywhere. 尚未在其他设备上对其进行过测试,但我认为它应该可以在任何地方使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM