检测iPad方向变化

时间:2011-06-06 09:01:58

标签: javascript jquery iphone ipad screen-orientation

当用户将iPad从垂直位置转为水平位置或从水平位置转为垂直位置时,如何使用进行检测?

4 个答案:

答案 0 :(得分:41)

尝试

$(window).bind('orientationchange', function(event) {
  alert('new orientation:' + event.orientation);
});

答案 1 :(得分:17)

您可以使用以下代码检测方向更改事件:

<强> jQuery的:

$(document).ready(function() {
    $(window).on('orientationchange', function(event) {
        console.log(orientation);
    });
});

检查设备是否处于纵向模式

function isPortrait() {
    return window.innerHeight > window.innerWidth;
}

答案 2 :(得分:13)

在Javascript中:

<button onclick="detectIPadOrientation();">What's my Orientation?</button>

<script type="text/javascript">
 window.onorientationchange = detectIPadOrientation;
 function detectIPadOrientation () {

    if ( orientation == 0 ) {
     alert ('Portrait Mode, Home Button bottom');
    }
    else if ( orientation == 90 ) {
     alert ('Landscape Mode, Home Button right');
    }
    else if ( orientation == -90 ) {
     alert ('Landscape Mode, Home Button left');
    }
    else if ( orientation == 180 ) {
     alert ('Portrait Mode, Home Button top');
    }
 }
</script>

或者包含其他样式表:

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

两者均来自:http://favo.asia/2010/07/detecting-ipad-orientation-using-javascript/,仅供参考,这是谷歌首次在'检测ipad方向javascript'上的结果......

答案 3 :(得分:0)

function detectIPadOrientation (orientation) {  
   if ( orientation == 0 ) {  
    alert ('Portrait Mode, Home Button bottom');  
   }  
   else if ( orientation == 90 ) {  
    alert ('Landscape Mode, Home Button right');  
   }  
   else if ( orientation == -90 ) {  
    alert ('Landscape Mode, Home Button left');  
   }  
   else if ( orientation == 180 ) {  
    alert ('Portrait Mode, Home Button top');  
   }  
}

window.onorientationchange = detectIPadOrientation;