2011-03-31 110 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