2012-10-02 16 views
1

我正在使用iPad中的方向。我正在使用電話差距來構建應用程序。 我已經做了以下的方向改變代碼:在javascript中使用iPad的方向

 function updateOrientation() 
     { 
      var orientation=window.orientation; 
      if (orientation == 0) { 
       alert("Do Something In Portrait Mode"); 
      } 
      else if (orientation == 90) { 
       alert("Do Something In landscape Mode"); 
      } 
      else if (orientation == -90) { 
       alert("Do Something In Portrait Mode"); 
      } 
      else if (orientation == 180) { 
       alert("Do Something In landscape Mode"); 
      }    
     } 

我收到警報,但我不知道如何來調整這裏面的窗口?我想要如下: enter image description here

+0

你有沒有考慮使用像jQuery Mobile的庫? :S它可以讓事情變得更容易我猜... – Littm

+0

沒有,你可以請給一些更多的信息。] –

+0

jQuery Mobile是一個javascript lib,它可以幫助你創建手機的網絡應用程序。它提供原生類組件,並具有根據設備的方向進行調整的特殊設計。你可以看看它:http://jquerymobile.com/ – Littm

回答

2

Safari和Android都支持簡單的方向更改事件偵聽器。

<!DOCTYPE HTML> 
<html> 
<head> 
<meta name="viewport" content="width=320; user-scalable=no" /> 
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> 
<title>PhoneGap</title> 
    <script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script> 
    <script type="text/javascript" charset="utf-8" src="main.js"></script> 

</head> 
<body onload="init();" > 

    <h2>Orientation Test</h2> 
    <div id="status"></div> 

</body> 
</html> 

和JS代碼:

function init() { 
    window.addEventListener("orientationchange", orientationChange, true); 
} 

function orientationChange(e) { 
    var orientation="portrait"; 
    if(window.orientation == -90 || window.orientation == 90) orientation = "landscape"; 
     document.getElementById("status").innerHTML+=orientation+"<br>"; 
} 

Source