3

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

我在 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;
    }
   }
  }

带有以下正文标签:

<body onorientationchange="updateOrientation();">

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

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

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

谢谢你的帮助!

4

1 回答 1

3

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

以下 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;
        }
    }

更新:两年多以前,旧版本的 Phonegap 和 iOS SDK 就可以使用,但根据一些用户的说法,它不再起作用了。

于 2010-08-23T14:14:10.483 回答