2011-10-18 30 views
1
window.onresize = function(){ updateOrientation(e); } 
    function updateOrientation(e) { 
     alert("hey"); 

     deviceWidth = $('body').width(); 
     if (deviceWidth == 320) { 
      currentOrientation = "portrait"; 
     } 
     else if (deviceWidth == 480) { 
      currentOrientation = "landscape"; 
     } 

     // fire a function that checks the orientation every x milliseconds 
     setInterval(checkOrientation, 500); 

     // check orientation 
     function checkOrientation() { 
      deviceWidth = $('body').width(); 
      if (deviceWidth >= '1200') { 
       newOrientation = "portrait"; 
      } 
      else if (deviceWidth <= '900') { 
       newOrientation = "landscape"; 
      } 
      // if orientation changed since last check, fire either the portrait or landscape function 
      if (newOrientation != currentOrientation) { 
       if (newOrientation == "portrait") { 
        changedToPortrait(); 
       } 
       else if (newOrientation == "landscape") { 
        changedToLandscape(); 
       } 
       currentOrientation = newOrientation; 
      } 
     } 

     // landscape stuff 
     function changedToLandscape() { 
      alert('Orientation has changed to Landscape!'); 
     } 

     // portrait stuff 
     function changedToPortrait() { 
      alert('Orientation has changed to Portrait!'); 
     } 
    } 

我正在使用此功能使用phonegap檢測android平板電腦中的方向更改。不知何故代碼不起作用。有什麼建議麼?它無法爲猜測的方向變化生成事件。電話中的方向更改

+0

@庫什 - 您是否使用了與PhoneGap的沿JQM框架來開發一個Android應用程序? – Mkpatel

回答

0

這工作對我的PhoneGap:

function onOrientationChange() { 
    switch (window.orientation) { 
     case -90: 
     case 90: 
      alert('landscape'); 
      break; 
     default: 
      alert('portrait'); 
      break; 
    } 
}; 
window.onresize = onOrientationChange;