Android 3.0 Web Dev 初始方向问题

发布于 2024-10-30 18:24:34 字数 1313 浏览 1 评论 0原文

我注意到 Android(到目前为止 2.2 和 3.0)设置 window.orientation javascript 变量的方式存在问题。当您最初加载网页时,一切正常,window.orientation 已正确设置为设备所在的方向(0, 90, -90, 180)。当您通过 window.open("newpage") javascript 调用启动新页面时,就会出现问题。当新页面打开时,无论您处于哪个方向,window.orientation 值始终为 0。下面是可以演示这一点的示例网页:

<button onclick="window.open('AndroidTesting.html')">Click Me</button>

    <script type="text/javascript">

    orientationEvent = "resize";
    window.addEventListener(orientationEvent, function () {
        setTimeout(displayChange(), 0);
    }, false);

        var myPageWidth = 0;
        var myPageHeight = 0;

        var lastOrientation = "Landscape";

        function displayChange() {

            if ((window.orientation == 90) || (window.orientation == -90)) {
                lastOrientation = "Portrait";
            }
            if ((window.orientation == 0) || (window.orientation == 180)) {
                lastOrientation = "Landscape";
            }

            document.getElementById('storydiv').innerHTML = "In " + lastOrientation + " mode";

        }

    </script>

当您单击按钮在新选项卡中打开同一页面时,如果您未处于默认模式(Android 2.2 默认模式或 0 角度是纵向,在 Android 3.0 中 0 角度是横向),则方向是错误的,但是一旦您旋转到其他模式,然后恢复方向工作。

以前有人见过这个问题吗?有解决办法吗,或者这只是因为我做错了什么而出现的问题?

感谢您的帮助!

I have noticed an issue with the way Android (so far both 2.2 and 3.0) sets the window.orientation javascript variable. When you initially load a web page everything works fine window.orientation is correctly set to the orientation the device is in (0, 90, -90, 180). The problem arises when you launch a new page via a window.open("newpage") javascript call. When the new page opens the window.orientation value is always 0 no matter what orientation you are in. Below is a sample webpage that can demonstrate this:

<button onclick="window.open('AndroidTesting.html')">Click Me</button>

    <script type="text/javascript">

    orientationEvent = "resize";
    window.addEventListener(orientationEvent, function () {
        setTimeout(displayChange(), 0);
    }, false);

        var myPageWidth = 0;
        var myPageHeight = 0;

        var lastOrientation = "Landscape";

        function displayChange() {

            if ((window.orientation == 90) || (window.orientation == -90)) {
                lastOrientation = "Portrait";
            }
            if ((window.orientation == 0) || (window.orientation == 180)) {
                lastOrientation = "Landscape";
            }

            document.getElementById('storydiv').innerHTML = "In " + lastOrientation + " mode";

        }

    </script>

When you click the button to open the same page in a new tab the orientation is wrong if you are not in the default mode (Android 2.2 the default mode or 0 angle is portrait in Android 3.0 the 0 angle is landscape), but once you rotate to the other mode and then back the orientation works.

Has anyone seen this issue before? Is there a work around, or is it just a problem because I am doing something wrong?

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雨成花 2024-11-06 18:24:34

我不知道这是否是一个好方法,但似乎在 Android 上, screen.width 和 screen.height 取决于设备方向。

if(android){
    if(screen.width>screen.height){
        return 90;
    }else{
        return 0;
    }
}else{
    return window.orientation;
}

该解决方案的主要问题在于操作系统检测。

I don't know if it's a good approach, but it seems that on Android, screen.width and screen.height depends of the device orientation.

if(android){
    if(screen.width>screen.height){
        return 90;
    }else{
        return 0;
    }
}else{
    return window.orientation;
}

The main problem of this solution is in the os detection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文