我能否使用Javascript在我的页面上检测到用户以纵向模式使用移动设备访问,并在用户将其手机旋转到横向时停止方向更改? 我的页面上有一款游戏,仅针对纵向显示进行了优化,我不希望它在横向模式下显示。
我能否使用Javascript在我的页面上检测到用户以纵向模式使用移动设备访问,并在用户将其手机旋转到横向时停止方向更改? 我的页面上有一款游戏,仅针对纵向显示进行了优化,我不希望它在横向模式下显示。
新的API正在开发中(目前已经可用)!
screen.orientation.lock(); // webkit only
和
screen.lockOrientation("orientation");
“方向”可以是以下任意一种:
portrait-primary - 表示屏幕在其主要纵向模式下的方向。如果设备以垂直方式保持在其正常位置,并且该位置为纵向,则被认为屏幕处于其主纵向模式。或者,如果设备的正常位置为横向并且将其顺时针旋转90度,则设备被认为处于其主纵向模式中。 正常位置取决于设备。
portrait-secondary - 表示屏幕在其次要纵向模式下的方向。如果将设备从其正常位置反转180º并且该位置为纵向,则被认为屏幕处于其次要纵向模式。或者,如果设备的正常位置为横向并将其逆时针旋转90度,则设备被认为处于其次要纵向模式中。 正常位置取决于设备。
landscape-primary - 表示屏幕在其主要横向模式下的方向。如果设备以横向方式保持在其正常位置,并且该位置为横向,则被认为屏幕处于其主横向模式。或者,如果设备的正常位置为纵向并将其顺时针旋转90度,则设备被认为处于其主横向模式中。 正常位置取决于设备。
landscape-secondary - 表示屏幕在其次要横向模式下的方向。如果将设备从其正常位置反转180º并且该位置为横向,则被认为屏幕处于其次要横向模式。或者,如果设备的正常位置为纵向并将其逆时针旋转90度,则设备被认为处于其次要横向模式中。 正常位置取决于设备。
portrait - 表示纵向模式(包括主要和次要)。
landscape - 表示横向模式(包括主要和次要)。
default - 表示根据设备的自然方向选择纵向或横向(主要模式)。例如,如果面板分辨率为1280 * 800,默认值将使其成为横向,如果分辨率为800 * 1280,则默认值将使其成为纵向。
Mozilla建议向屏幕添加lockOrientationUniversal以使其更具跨浏览器兼容性。
screen.lockOrientationUniversal = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation;
获取更多信息,请访问此处(已弃用的API):https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockOrientation
现代API文档在MDN的这里: https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/lock
$(window).bind('orientationchange resize', function(event){
if (event.orientation) {
if (event.orientation == 'landscape') {
if (window.rotation == 90) {
rotate(this, -90);
} else {
rotate(this, 90);
}
}
}
});
function rotate(el, degs) {
iedegs = degs/90;
if (iedegs < 0) iedegs += 4;
transform = 'rotate('+degs+'deg)';
iefilter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+iedegs+')';
styles = {
transform: transform,
'-webkit-transform': transform,
'-moz-transform': transform,
'-o-transform': transform,
filter: iefilter,
'-ms-filter': iefilter
};
$(el).css(styles);
}
rads = degs * Math.PI / 180;
m11 = m22 = Math.cos(rads);
m21 = Math.sin(rads);
m12 = -m21;
iefilter = "progid:DXImageTransform.Microsoft.Matrix("
+ "M11 = " + m11 + ", "
+ "M12 = " + m12 + ", "
+ "M21 = " + m21 + ", "
+ "M22 = " + m22 + ", sizingMethod = 'auto expand')";
styles['filter'] = styles['-ms-filter'] = iefilter;
—或者使用CSS Sandpaper。此外,这将应用旋转样式到窗口对象,我实际上从未测试过,也不知道是否有效。您可能需要将样式应用于文档元素。
无论如何,我仍然建议简单地显示一条消息,要求用户在纵向模式下玩游戏。
$(window).bind
函数中漏掉了一个闭合括号,并且在 rotation()
函数中有一个多余的括号。 - Raptorwindow.rotation
。我能找到的所有参考都是关于 window.orientation
的。我可能打错了字,或者可用的 API 在7年前是不同的。等我有空闲时间时,我将尝试调试并修订代码。 - Lèse majesté似乎很奇怪没有人提出CSS媒体查询解决方案:
@media screen and (orientation: portrait) {
...
}
同时提供使用特定样式表的选项:
<link rel="stylesheet" type="text/css" href="css/style_p.css" media="screen and (orientation: portrait)">
MDN: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries#orientation
style_p.css
样式表,您不应该为主要的样式表添加它。 - Facundo Colombier简单的Javascript代码可以让移动浏览器在纵向或横向显示。
(虽然在两个DIV中必须输入html代码两次(每种模式一个),但可以说这比使用JavaScript更改样式表加载速度更快...)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Mobile Device</title>
<script type="text/javascript">
// Detect whether device supports orientationchange event, otherwise fall back to
// the resize event.
var supportsOrientationChange = "onorientationchange" in window,
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
window.addEventListener(orientationEvent, function() {
if(window.orientation==0)
{
document.getElementById('portrait').style.display = '';
document.getElementById('landscape').style.display = 'none';
}
else if(window.orientation==90)
{
document.getElementById('portrait').style.display = 'none';
document.getElementById('landscape').style.display = '';
}
}, false);
</script>
<meta name="HandheldFriendly" content="true" />
<meta name="viewport" content="width=device-width, height=device-height, user-scalable=no" />
</head>
<body>
<div id="portrait" style="width:100%;height:100%;font-size:20px;">Portrait</div>
<div id="landscape" style="width:100%;height:100%;font-size:20px;">Landscape</div>
<script type="text/javascript">
if(window.orientation==0)
{
document.getElementById('portrait').style.display = '';
document.getElementById('landscape').style.display = 'none';
}
else if(window.orientation==90)
{
document.getElementById('portrait').style.display = 'none';
document.getElementById('landscape').style.display = '';
}
</script>
</body>
</html>
已在Android HTC Sense和Apple iPad上进行测试并可用。
通过新的CSS3特性,您可以旋转页面以相反的方向旋转。很抱歉,不支持IE7-。:(.
var rotate = 0 - window.orientation;
setAttribute("transform:rotate("+rotate+"deg);-ms-transform:rotate("+rotate+"deg);-webkit-transform:rotate("+rotate+"deg)", "style");
@media screen and (min-width: 320px) and (max-width: 767px) and (orientation: landscape) {
html {
transform: rotate(-90deg);
transform-origin: left top;
width: 100vh;
overflow-x: hidden;
position: absolute;
top: 100%;
left: 0;
}
}
在这里找到它:https://css-tricks.com/snippets/css/orientation-lock/
#rotate-device {
width: 100%;
height: 100%;
position: fixed;
z-index: 9999;
top: 0;
left: 0;
background-color: #000;
background-image: url(/path to img/rotate.png);
background-size: 100px 100px;
background-position: center;
background-repeat: no-repeat;
display: none;
}
@media only screen and (max-device-width: 667px) and (min-device-width: 320px) and (orientation: landscape){
#rotate-device {
display: block;
}
}
<div id="rotate-device"></div>
您可以使用screenSize.width和screenSize.height属性,并检测宽度>高度的情况,然后处理该情况,可以通过让用户知道或根据需要调整屏幕来实现。
但最好的解决方案是@Doozer1979所说的...为什么要覆盖用户的偏好?