在移动设备上,是否有可能将视口方向锁定为纵向?
我通过谷歌搜索了一下,但没有找到确切的方法。
这个技巧应该有效:
@media screen and (orientation: landscape) {
html {
/* Rotate the content container */
transform: rotate(-90deg);
transform-origin: left top;
/* Set content width to viewport height */
width: 100vh;
/* Set content height to viewport width */
height: 100vw;
overflow-x: hidden;
position: absolute;
top: 100%;
left: 0;
}
}
<h1>Test</h1>
is-mobile-device
或其他类似的名称,并将html.is-mobile-device
指定为样式定义中的选择器。您可能已经使用了一个框架,它为不同的设备类型添加了类,因此您可以指定这些类。请注意,这实际上并不会锁定方向,只是使页面看起来像被锁定了。使用CSS,我们可以使用;这在iPhone上可能有效:
@viewport {
orientation: portrait;
}
这里有一个链接,我们可以添加它,基本上做的是相同的事情:
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
screen.lockOrientation('portrait');
这是我们记录方向的方法:
screen.addEventListener("orientationchange", function () {
console.log("The orientation of the screen is: " + screen.orientation);
});
screen.lockOrientation('portrait');
无法使用。 - H. Ferrence/*
CSS: For most of the phones
812px - for iphone x
*/
@media only screen and (pointer: coarse) and (min-width: 320px) and (max-width: 812px) and (orientation: landscape) {
html {
transform: rotate(-90deg);
transform-origin: left top;
width: 100vh;
overflow-x: hidden;
position: absolute;
top: 100%;
left: 0;
}
}
的意思是保留HTML标签,其中包含一个指向MDN中关于@media - pointer support的链接。@media screen and (orientation: landscape) {
CSS here.
}
如果你使用的是功能强大的设备并且处于全屏状态,那么通过屏幕方向 API是可以实现的。
2023年10月,它在安卓设备上得到了支持,但苹果设备尚未支持。
HTML
<button id="btnNullscreen">Fullscreen</button>
<button id="btnLandscape">Landscap</button>
<button id="btnPortrait">Portrait</button>
JAVASCRIPT
btnNullscreen.onclick = () => document.body.requestFullscreen().catch((e) => alert(e));
btnLandscape.onclick = () => screen.orientation.lock('landscape').catch((e) => alert(e));
btnPortrait.onclick = () => screen.orientation.lock('portrait').catch((e) => alert(e));