Detect iPad orientation change

2019-01-31 16:37发布

How to detect with or when user turns iPad from vertical position to horizontal or from horizontal to vertical?

4条回答
贪生不怕死
2楼-- · 2019-01-31 17:13
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;

查看更多
聊天终结者
3楼-- · 2019-01-31 17:22

Try

$(window).bind('orientationchange', function(event) {
  alert('new orientation:' + event.orientation);
});
查看更多
做个烂人
4楼-- · 2019-01-31 17:28

In 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>

Or for including additional stylesheets:

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

Both taken from: http://favo.asia/2010/07/detecting-ipad-orientation-using-javascript/ which, FYI, was the first result on Google for 'detect ipad orientation javascript'...

查看更多
Root(大扎)
5楼-- · 2019-01-31 17:29

You can detect the orientation change event using the following code:

jQuery:

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

Check if device is in portrait mode

function isPortrait() {
    return window.innerHeight > window.innerWidth;
}
查看更多
登录 后发表回答