【发布时间】:2013-01-16 14:25:29
【问题描述】:
我正在尝试为我的应用程序强制使用“横向”模式,因为我的应用程序绝对不是为“纵向”模式设计的。 我怎样才能做到这一点?
【问题讨论】:
标签: javascript device-orientation
我正在尝试为我的应用程序强制使用“横向”模式,因为我的应用程序绝对不是为“纵向”模式设计的。 我怎样才能做到这一点?
【问题讨论】:
标签: javascript device-orientation
现在可以使用 HTML5 webapp 清单。见下文。
原答案:
您无法将网站或 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">
不完全确定 webapp 清单上对锁定方向模式的支持是什么,但 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;
}
}
【讨论】:
landscape 更改为portrait,它按预期工作@media screen and (orientation: portrait)
portrait 时应应用旋转(以便可以切换到landscape)。
vh 更改为 vw 和 vw 更改为 vh ......所以如果有人没有成功,可能是因为它。
我有同样的问题,它是缺少 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 效果很好,希望对您有所帮助!
【讨论】: