2011-03-31 48 views

回答

22

UPDATE:

你可能想看看

jQuery mobile orientationchange

或普通JS一:

window.addEventListener("orientationchange", function() { 
alert(window.orientation); 
}, false); 

年長的答案

http://www.nczonline.net/blog/2010/04/06/ipad-web-development-tips/

Safari瀏覽器在iPad上不支持的window.orientation屬性,所以如果需要的話,你可以用它來確定用戶是否在水平或垂直模式。作爲對此功能的提醒:

window.orientation is 0 when being held vertically 
window.orientation is 90 when rotated 90 degrees to the left (horizontal) 
window.orientation is -90 when rotated 90 degrees to the right (horizontal) 

當設備旋轉時,還會觸發窗口對象上的orientationchange事件。

您還可以使用CSS媒體查詢,以確定是否在垂直或水平方向正在舉行的iPad,比如:

<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css"> 
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css"> 

http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3889591/Detect-and-Set-the-iPhone--iPads-Viewport-Orientation-Using-JavaScript-CSS-and-Meta-Tags.htm

<script type="text/javascript"> 
var updateLayout = function() { 
    if (window.innerWidth != currentWidth) { 
    currentWidth = window.innerWidth; 
    var orient = (currentWidth == 320) ? "profile" : "landscape"; 
    document.body.setAttribute("orient", orient); 
    window.scrollTo(0, 1); 
    } 
}; 

iPhone.DomLoad(updateLayout); 
setInterval(updateLayout, 400); 
</script> 
+0

看來,從iOS 4.0開始,UIWebView版本不再支持Javascript window.orientation屬性,或者至少我已經體驗過了。所以,我發現通知UIWebView的內部Javascript有關方向更改的唯一方法是調用[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@「didRotateTo('%@')」,orient]];其中webView是我的UIWebView,orient是當前設備方向的字符串表示。 didRotateTo是一個javascript函數,它接受一個參數(方向爲字符串) – 2011-04-28 10:05:44

+0

對於輪詢,有一個'orientationchange'事件在設備的方向更改時觸發。 – alex 2012-07-04 06:16:59

0

window.orientation是你在找什麼。這裏還有一個onOrientationChange事件 作品爲Android,iPhone和,我主要肯定的是,iPad的

0

添加到@ mplungjan答案,我發現使用webkit「本地」(我真的不怎麼稱呼它)事件,'deviceorientation' 。

Mozilla Developer network他們有一個很好的解釋,如何規範webkit和Gecko之間的幫助我解決這個問題。

5

可以使用orientationChange事件,像這樣:

window.addEventListener('orientationchange', function(){ 
    /* update layout per new orientation */ 
}); 
+0

這個事件已經被討論過了。 – 2012-09-22 00:22:05

0

您可以使用mediaMatch評估CSS媒體查詢,例如

window 
    .matchMedia('(orientation: portrait)') 
    .addListener(function (m) { 
     if (m.matches) { 
      // portrait 
     } else { 
      // landscape 
     } 
    }); 

CSS媒體查詢在orientationchange之前觸發。如果您想捕捉事件的結束(旋轉完成時),請參閱mobile viewport height after orientation change

0

一個易於使用的片段:

function doOnOrientationChange() 
{ 
    switch(window.orientation) 
    { 
     case -90: 
     case 90: 
      // alert('landscape'); 
      $('#portrait').css({display:'none'}); 
      $('#landscape').css({display:'block'}); 

      break; 
     default: 
      // alert('portrait'); 
      $('#portrait').css({display:'block'}); 
      $('#landscape').css({display:'none'}); 
      break; 
    } 
} 

window.addEventListener('orientationchange', doOnOrientationChange); 

// First launch 
doOnOrientationChange(); 
0

從「Cross-device, cross-browser portrait-landscape detection

這是一個關於找出一個移動設備是處於縱向或橫向模式;你不需要關心它的方向。對於所有你知道的,如果你把你的iPad倒過來,它是在肖像模式。

$(window).bind("resize", function(){ 
    screenOrientation = ($(window).width() > $(window).height())? 90 : 0; 
}); 

90代表景觀,0代表縱向,跨瀏覽器,跨設備。

window.onresize事件隨處可用,它總是在正確的時間觸發;不要太早,永遠不要太晚。事實上,屏幕的大小也總是準確的。

+0

用戶如何以縱向方向握住手機,然後打開虛擬鍵盤,導致寬度變得更大,然後在小屏幕上顯示高度,情況如何?用戶仍然以相同的縱向方向握住手機,但是您的方法可能會導致方向更改的錯誤結果。 – 2016-07-26 15:22:12