Detect iPad orientation change

╄→гoц情女王★ 提交于 2019-11-29 22:13:22

Try

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

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;
}

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'...

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;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!