如何在JavaScript/jQuery中找出iPad是否处于景观/肖像模式?
-
29-09-2019 - |
题
如果iPad处于景观模式,我想添加额外的DIV。是否有某种可以找到这种情况的语句?
谢谢
解决方案
JQTouch检查一下:
orientation = Math.abs(window.orientation) == 90 ? 'landscape' : 'portrait';
http://github.com/senchalabs/jqtouch/blob/master/jqtouch/jqtouch.js
您也可以收听OnErientationChange事件
请参阅上一个答案: 检测使用JavaScript在浏览器中旋转的旋转
其他提示
您可以简单地检查文档宽度。
$(window).width();
您可以将其设置为变量,然后检查iPad的本机分辨率的变量:肖像中的768px x 1024px。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Rotation Test</title>
<link type="text/css" href="css/style.css" rel="stylesheet"></style>
<script src="js/jquery-1.5.min.js" type="text/javascript"></script>
<script type="text/javascript">
window.addEventListener("resize", function() {
// Get screen size (inner/outerWidth, inner/outerHeight)
var height = $(window).height();
var width = $(window).width();
if(width>height) {
// Landscape
$("#mode").text("LANDSCAPE");
} else {
// Portrait
$("#mode").text("PORTRAIT");
}
}, false);
</script>
</head>
<body onorientationchange="updateOrientation();">
<div id="mode">LANDSCAPE</div>
</body>
</html>
您可以尝试与所有浏览器兼容的解决方案。
以下是 orientationchange
兼容性图片:因此,我撰写了 orientaionchange
polyfill,它是基于@media属性来修复entientationChange实用程序库 -entientationchange-fix
window.addEventListener('orientationchange', function(){
if(window.neworientation.current === 'portrait|landscape'){
// do something……
} else {
// do something……
}
}, false);
然后,您可以通过 window.neworientation.current
和初始定向状态 window.neworientation.init
.
不隶属于 StackOverflow