Detect iPad orientation change

断了今生、忘了曾经 提交于 2019-11-28 18:53:56

问题


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


回答1:


Try

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



回答2:


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



回答3:


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




回答4:


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;



来源:https://stackoverflow.com/questions/6249722/detect-ipad-orientation-change

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