javascript - 强制 “landscape” 方向模式

标签 javascript css device-orientation

我试图为我的应用程序强制使用“横向”模式,因为我的应用程序绝对不是为“纵向”模式设计的。 我怎样才能做到这一点?

最佳答案

现在可以使用 HTML5 webapp list 。见下文。


原答案:

您无法在特定方向锁定网站或网络应用程序。这违背了设备的自然行为。

您可以像这样使用 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
}

或者像这样绑定(bind)一个 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 webapp list 。

html5rocks.com 中所述,您现在可以使用 manifest.json 文件强制定向模式。

您需要将这些行包含到 json 文件中:

{
    "display":      "standalone", /* Could be "fullscreen", "standalone", "minimal-ui", or "browser" */
    "orientation":  "landscape", /* Could be "landscape" or "portrait" */
    ...
}

并且您需要像这样将 list 包含到您的 html 文件中:

<link rel="manifest" href="manifest.json">

不确定 webapp list 上的锁定方向模式支持什么,但 Chrome 肯定存在。当我有信息时会更新。

关于javascript - 强制 “landscape” 方向模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17057733/

相关文章:

Javascript 将数组插入对象内部

javascript - componentDidMount 意外 token 错误 [React.js]

C# HtmlEncode,然后使用 .innerHTML Javascript 插入

css - Dreamweaver CS5 - CSS 网站设计工具

android - Google Play 游戏服务多人设备方向更改将用户踢出房间

android - 应用内计费 - 快速设备定位 - 导致崩溃(IllegalStateException)

javascript - 为什么每次调用函数时都不会重置变量

javascript - 从中心向右动画div(css或jquery)

html - 更少的 CSS - & :focus not working in variable selector

ios - 在纵向状态下强制旋转 UIViewController