我正在尝试强制应用程序进入“横屏”模式,因为我的应用程序绝对不是为“竖屏”模式设计的。 我该怎么做?
我该怎样强制应用程序运行时只允许在横屏模式下显示呢?
我正在尝试强制应用程序进入“横屏”模式,因为我的应用程序绝对不是为“竖屏”模式设计的。 我该怎么做?
我该怎样强制应用程序运行时只允许在横屏模式下显示呢?
现在有了HTML5 网页应用程序清单,这是可能的。请参见下文。
原始回答:
您无法将网站或Web应用程序锁定在特定方向上。 这违反了设备的自然行为。
您可以使用CSS3媒体查询来检测设备方向,如下所示:
@media screen and (orientation:portrait) {
// CSS applied when the device is in portrait mode
}
@media screen and (orientation:landscape) {
// CSS applied when the device is in landscape mode
}
或者通过像这样绑定JavaScript方向更改事件:
document.addEventListener("orientationchange", function(event){
switch(window.orientation)
{
case -90: case 90:
/* Device is in landscape mode */
break;
default:
/* Device is in portrait mode */
}
});
2014年11月12日更新:现在可以通过HTML5网络应用清单实现。
正如在html5rocks.com中所解释的,您现在可以使用manifest.json
文件强制指定方向模式。
您需要在json文件中包含以下行:
{
"display": "standalone", /* Could be "fullscreen", "standalone", "minimal-ui", or "browser" */
"orientation": "landscape", /* Could be "landscape" or "portrait" */
...
}
你需要像这样将清单包含在你的 HTML 文件中:
<link rel="manifest" href="manifest.json">
不确定 Web 应用程序清单中支持锁定方向模式的具体情况,但 Chrome 明确支持。在我获得信息后会进行更新。
screen.orientation.lock('landscape');
将其强制更改并保持横向模式。已在Nexus 5上测试。
我使用的一些 CSS 如下所示(基于 css tricks):
@media screen and (min-width: 320px) and (max-width: 767px) and (orientation: portrait) {
html {
transform: rotate(-90deg);
transform-origin: left top;
width: 100vh;
height: 100vw;
overflow-x: hidden;
position: absolute;
top: 100%;
left: 0;
}
}
@media screen and (orientation: portrait)
。 - ymzvh
更改为vw
,将每个vw
更改为vh
(在纵向模式下)...所以如果有人没有成功,可能是因为这个原因。 - tomer raitz我曾经遇到同样的问题,那是因为缺少 manifest.json 文件。如果没有这个文件,浏览器会自行决定最适合的方向,如果您没有指定文件或使用错误的路径。
我只需正确地在 HTML 标头中调用 manifest.json 即可解决问题。
我的 HTML 标头:
<meta name="application-name" content="App Name">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="manifest" href="manifest.json">
<meta name="msapplication-starturl" content="/">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#">
<meta name="msapplication-TileColor" content="#">
<meta name="msapplication-config" content="browserconfig.xml">
<link rel="icon" type="image/png" sizes="192x192" href="android-chrome-192x192.png">
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
<link rel="mask-icon" href="safari-pinned-tab.svg" color="#ffffff">
<link rel="shortcut icon" href="favicon.ico">
并且 manifest.json 文件的内容:
{
"display": "standalone",
"orientation": "portrait",
"start_url": "/",
"theme_color": "#000000",
"background_color": "#ffffff",
"icons": [
{
"src": "android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
}
}