我正在尝试为我的应用程序强制“横向”模式,因为我的应用程序绝对不是为“纵向”模式设计的。 我怎样才能做到这一点 ?
有什么建议吗? 感谢
答案 0 :(得分:66)
现在可以使用HTML5 webapp清单。见下文。
原始回答:
您无法以特定方向锁定网站或网络应用程序。这违背了设备的自然行为。
您可以使用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 */
}
});
11月12日更新:现在可以使用HTML5 webapp清单。
正如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">
不完全确定webapp清单上的支持对于锁定方向模式,但Chrome绝对存在。我有信息时会更新。
答案 1 :(得分:34)
screen.orientation.lock('landscape');
将强制它更改为并保持横向模式。在Nexus 5上测试过。
答案 2 :(得分:4)
我使用这样的CSS(基于css tricks):
@media screen and (min-width: 320px) and (max-width: 767px) and (orientation: landscape) {
html {
transform: rotate(-90deg);
transform-origin: left top;
width: 100vh;
height: 100vw;
overflow-x: hidden;
position: absolute;
top: 100%;
left: 0;
}
}
答案 3 :(得分:0)
我遇到了同样的问题,那是一个缺少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"
}
}
要生成您的网站图标和图标,请使用此网络工具: https://realfavicongenerator.net/
要生成清单文件,请使用: https://tomitm.github.io/appmanifest/
我的PWA很好,希望对您有帮助!