【发布时间】:2021-04-06 20:51:14
【问题描述】:
是否可以使用 javascript 检测 iPad 或 Galaxy Tab 上浏览器方向的变化?我认为可以使用 css 媒体查询。
【问题讨论】:
标签: javascript css ipad accelerometer galaxy-tab
是否可以使用 javascript 检测 iPad 或 Galaxy Tab 上浏览器方向的变化?我认为可以使用 css 媒体查询。
【问题讨论】:
标签: javascript css ipad accelerometer galaxy-tab
let theDeviceIsRotated;
function handlePortraitOrLandscape() {
setTimeout(afterAnUnnoticableDelay,100); // This solves the wrong-firing-order issue on Samsung Browser.
function afterAnUnnoticableDelay() {
if (screen.orientation) { // Mainly for Android (as of 2021)
// document.getElementById('or7on').innerHTML = screen.orientation.angle; // Returns 0 or 90 or 270 or 180 // Create a div with ID: "or7on" and uncomment to test
if (screen.orientation.angle == 0) { theDeviceIsRotated="no"; }
if (screen.orientation.angle == 90) { theDeviceIsRotated="toTheLeft"; }
if (screen.orientation.angle == 270) { theDeviceIsRotated="toTheRight"; }
if (screen.orientation.angle == 180) { theDeviceIsRotated="upsideDown"; }
} else { // Mainly for iOS (as of 2021)
// document.getElementById('or7on').innerHTML = window.orientation; // Returns 0 or 90 or -90 or 180 // Create a div with ID: "or7on" and uncomment to test
if (window.orientation == 0) { theDeviceIsRotated="no"; }
if (window.orientation == 90) { theDeviceIsRotated="toTheLeft"; }
if (window.orientation == -90) { theDeviceIsRotated="toTheRight"; }
if (window.orientation == 180) { theDeviceIsRotated="upsideDown"; }
}
}
}
handlePortraitOrLandscape(); // Set for the first time
window.addEventListener("resize",handlePortraitOrLandscape); // Update when change happens
【讨论】:
你可以像这样使用orientationchange事件:
window.addEventListener('orientationchange', function(event) {
/* update layout per new orientation */
});
【讨论】:
我意识到在这个线程中没有人提到当设备被倒置时会发生什么。
window.orientation 在水平放置时返回 -90 或 90。垂直保持时返回 0 或 180。有些设备支持,有些不支持倒置。我推荐,
window.addEventListener("orientationchange", function() {
if ( window.orientation == 0 || window.orientation == 180) {
// WHEN IN PORTRAIT MODE
} else {
// WHEN IN LANDSCAPE MODE
}
}, false);
另请注意,window.orientation 在桌面上返回 undefined。
【讨论】:
更新:
你可能想看看
jQuery mobile orientationchange
或普通的 JS 之一:
window.addEventListener("orientationchange", function() {
alert(window.orientation);
}, false);
window.addEventListener("orientationchange", function() {
alert("the orientation of the device is now " + screen.orientation.angle);
});
旧答案
http://www.nczonline.net/blog/2010/04/06/ipad-web-development-tips/
iPad 上的 Safari 确实支持 window.orientation 属性,因此如有必要,您可以使用它来确定用户是处于水平模式还是垂直模式。提醒一下这个功能:
window.orientation is 0 when being held vertically
window.orientation is 90 when rotated 90 degrees to the left (horizontal)
window.orientation is -90 when rotated 90 degrees to the right (horizontal)
还有一个orientationchange事件,当设备旋转时会在window对象上触发。
您还可以使用 CSS 媒体查询来确定 iPad 是处于垂直还是水平方向,例如:
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
<script type="text/javascript">
var updateLayout = function() {
if (window.innerWidth != currentWidth) {
currentWidth = window.innerWidth;
var orient = (currentWidth == 320) ? "profile" : "landscape";
document.body.setAttribute("orient", orient);
window.scrollTo(0, 1);
}
};
iPhone.DomLoad(updateLayout);
setInterval(updateLayout, 400);
</script>
【讨论】:
orientationchange 事件。
window.addEventListener("orientationchange", function() { console.log("the orientation of the device is now " + screen.orientation.angle); });
来自“Cross-device, cross-browser portrait-landscape detection”
这是关于确定移动设备是处于纵向还是横向模式;你不需要关心它的方向。如您所知,如果您将 iPad 倒置,则它处于纵向模式。
$(window).bind("resize", function(){
screenOrientation = ($(window).width() > $(window).height())? 90 : 0;
});
90 表示横向,0 表示纵向、跨浏览器、跨设备。
window.onresize 事件随处可用,并且总是在正确的时间触发;永远不会太早,永远不会太晚。事实上,屏幕的大小也总是准确的。
JavaScript 版本是这样的,如果我错了,请纠正我。
function getScreenOrientation() {
screenOrientation = window.outerWidth > window.outerHeight ? 90 : 0;
console.log("screenOrientation = " + screenOrientation);
}
window.addEventListener("resize", function(event) {
getScreenOrientation();
});
getScreenOrientation();
【讨论】:
一个易于使用的 sn-p :
function doOnOrientationChange()
{
switch(window.orientation)
{
case -90:
case 90:
// alert('landscape');
$('#portrait').css({display:'none'});
$('#landscape').css({display:'block'});
break;
default:
// alert('portrait');
$('#portrait').css({display:'block'});
$('#landscape').css({display:'none'});
break;
}
}
window.addEventListener('orientationchange', doOnOrientationChange);
// First launch
doOnOrientationChange();
【讨论】:
您可以使用mediaMatch 来评估 CSS 媒体查询,例如
window
.matchMedia('(orientation: portrait)')
.addListener(function (m) {
if (m.matches) {
// portrait
} else {
// landscape
}
});
CSS 媒体查询在 orientationchange 之前触发。如果您希望捕捉事件的结束(当轮换完成时),请参阅mobile viewport height after orientation change。
【讨论】:
除了@mplungjan 答案之外,我发现使用 webkit “native”(我不知道如何称呼它)事件“deviceorientation”获得了更好的结果。
在Mozilla Developer network 中,他们很好地解释了如何在 webkit 和 Gecko 之间进行规范化,帮助我解决了这个问题。
【讨论】:
window.orientation 是您正在寻找的。还有一个 onOrientationChange 事件 适用于 android、iphone,而且我很确定适用于 ipad
【讨论】: