如何使用 JavaScript 或 jQuery 检测用户在 iPad 上将屏幕从垂直方向转换为水平方向或从水平方向转换为垂直方向?
尝试
$(window).bind('orientationchange', function(event) {
alert('new orientation:' + event.orientation);
});
您可以使用以下代码检测方向更改事件:
jQuery:
$(document).ready(function() {
$(window).on('orientationchange', function(event) {
console.log(orientation);
});
});
检查设备是否处于竖屏模式
function isPortrait() {
return window.innerHeight > window.innerWidth;
}
console.log(event.orientation)
吗?(缺少 event) - David在 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/。需要说明的是,该网址是在Google上搜索“detect ipad orientation javascript”时的第一个结果...
orientation
而不是event.orientation
。 - tremby