Javascript 检测 iPad 方向变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6249722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect iPad orientation change
提问by lolalola
How to detect with javascriptor jquerywhen user turns iPad from vertical position to horizontal or from horizontal to vertical?
当用户将 iPad 从垂直位置变为水平或从水平变为垂直时,如何使用javascript或jquery进行检测?
回答by Karl-Bj?rnar ?ie
Try
尝试
$(window).bind('orientationchange', function(event) {
alert('new orientation:' + event.orientation);
});
回答by martynas
You can detect the orientation change event using the following code:
您可以使用以下代码检测方向更改事件:
jQuery:
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;
}
回答by Simon
In Javascript:
在 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'...
两者均取自:http: //favo.asia/2010/07/detecting-ipad-orientation-using-javascript/,仅供参考,这是 Google 上第一个“检测 ipad 方向 javascript”的结果...
回答by Fatih Acet
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;