Javascript 方向更改不适用于 iPad 上的 PhoneGap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3547860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:28:28  来源:igfitidea点击:

Javascript Orientation Change doesn't work with PhoneGap on iPad

javascriptiphoneipadcordova

提问by axooh

I'm adapting a small HTML/CSS/Javascript driven web magazine as an iApp for the iPad with the great mobile framework PhoneGap. Everything works great, except the orientation change from portrait to landscape mode.

我正在将一个小型 HTML/CSS/Javascript 驱动的网络杂志改编为 iPad 的 iApp,其中包含出色的移动框架 PhoneGap。一切都很好,除了从纵向模式到横向模式的方向变化。

I use the following Javascript code in the header of the index.html (portrait):

我在 index.html(纵向)的标题中使用以下 Javascript 代码:

  var isiPad = navigator.userAgent.match(/iPad/i) != null;
  var isiPhone = navigator.userAgent.match(/iPhone/i) != null;

  function updateOrientation() {
   if (isiPad || isiPhone) {
    switch (window.orientation) {
     case 90:
      window.location = "index_landscape.html";
      break;
     case -90:
      window.location = "index_landscape.html";
      break;
    }
   }
  }

with the following body tag:

带有以下正文标签:

<body onorientationchange="updateOrientation();">

If I rotate the iPad to landscape mode, it doesn't change to index-landscape.html.

如果我将 iPad 旋转到横向模式,它不会更改为 index-landscape.html。

Does anybody know where the problem is? Isn't it possible to change the HTML file, while changing orientation within the PhoneGap framework? With other words, can I only use oneHTML file (index.html) with PhoneGap and have to use CSS for the orientation change?

有人知道问题出在哪里吗?是否可以更改 HTML 文件,同时更改 PhoneGap 框架内的方向?换句话说,我可以在 PhoneGap 中只使用一个HTML 文件 (index.html) 并且必须使用 CSS 来改变方向吗?

If I check out the app as a website with the iPad Safari browser, the orientation change works properly.

如果我使用 iPad Safari 浏览器将应用程序作为网站查看,则方向更改可以正常工作。

Thanks for any help!

谢谢你的帮助!

采纳答案by axooh

I just found an other solution to the problem. I use the body onload function with the onDeviceReady function from phonegap.js to check the orientations:

我刚刚找到了该问题的其他解决方案。我使用 body onload 函数和 phonegap.js 中的 onDeviceReady 函数来检查方向:

The following javascript code works properly now:

以下 javascript 代码现在可以正常工作:

    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    function onDeviceReady() {
        document.addEventListener("orientationChanged", updateOrientation);
    }

    function updateOrientation(e) {
        switch (e.orientation) {
            case 90:
                window.location = "index_landscape.html";
                break;
            case -90:
                window.location = "index_landscape.html";
                break;
        }
    }

UPDATE: This worked more than two years ago with old versions of Phonegap and iOS SDK, but according some users, it doesn't work anymore.

更新:这在两年多前使用旧版本的 Phonegap 和 iOS SDK 有效,但据一些用户称,它不再有效。