使用 javascript 在 Android 中更改方向

发布于 2024-12-28 10:53:29 字数 268 浏览 0 评论 0原文

document.addEventListener("orientationChanged", updateOrientation);

我试图在 updateOrientation 上调用一个函数,但该函数只是没有被调用。我正在使用 javascript 代码来实现同样的目的。

任何人都可以帮助我使用 javascript 代码进行 orientationChange

提前致谢。

document.addEventListener("orientationChanged", updateOrientation);

I am trying to call a function on updateOrientation, but the function is just not called. I am using javascript code for the same.

Can anyone help me with orientationChange using javascript code.

Thanks in advance.

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

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

发布评论

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

评论(2

遥远的绿洲 2025-01-04 10:53:29

这并不容易:我也玩过很多次:)

首先,您可能已经注意到,某些 Android 设备(例如三星 Galaxy Tab)在横向时会被检测为纵向,反之亦然。因此,如果未检测到 ipad,首先您需要构建函数来根据 screen.width 和 screen.height 检测方向(ipad 将始终正确显示方向.. 无论如何,我对此并不感兴趣)。

然后,您必须在一段时间检测到方向变化并超时后触发回调函数,以使整个环境相应地根据新方向进行变化。

所以这就是我的做法..很乐意分享:)

function orientation_changed ()
{
    if ( is_portrait() )
    {
        //do something
    }
    else if ( is_landscape() )
    {
        // do something else
    }
    clearTimeout(window.t);
    delete window.t;
}

window.t = undefined;
window.onorientationchange = function (event)
{
    window.t = setTimeout('orientation_changed();', 250);
}

function is_landscape()
{
    var uagent = navigator.userAgent.toLowerCase();
    if ( uagent.search('ipad') > -1 )
    {
        var r = ( window.orientation == 90 || window.orientation == -90 );
    }
    else
    {
        var r = ( screen.width > screen.height );
    }
    return r;
}

function is_portrait()
{
    var uagent = navigator.userAgent.toLowerCase();
    if ( uagent.search('ipad') > -1 )
    {
        var r = ( window.orientation == 0 || window.orientation == 180 );
    }
    else
    {
        var r = ( screen.width < screen.height );
    }
    return r;
}

It's not so easy: I played around it a lot too :)

First you might have noticed that some android devices (for example samsung galaxy tab) will be detected as portrait while they are landscape and viceversa. So first you need to build functions to detect the orientation based on screen.width and screen.height if ipad is not detected (ipad will always show the orientation correctly .. I'm not a fun of it anyway).

Then you have to fire the callback function after a while that the orientation change is detected with a timeout to let the whole environment change accordingly to the new orientation.

So here is how I do .. happy to share it :)

function orientation_changed ()
{
    if ( is_portrait() )
    {
        //do something
    }
    else if ( is_landscape() )
    {
        // do something else
    }
    clearTimeout(window.t);
    delete window.t;
}

window.t = undefined;
window.onorientationchange = function (event)
{
    window.t = setTimeout('orientation_changed();', 250);
}

function is_landscape()
{
    var uagent = navigator.userAgent.toLowerCase();
    if ( uagent.search('ipad') > -1 )
    {
        var r = ( window.orientation == 90 || window.orientation == -90 );
    }
    else
    {
        var r = ( screen.width > screen.height );
    }
    return r;
}

function is_portrait()
{
    var uagent = navigator.userAgent.toLowerCase();
    if ( uagent.search('ipad') > -1 )
    {
        var r = ( window.orientation == 0 || window.orientation == 180 );
    }
    else
    {
        var r = ( screen.width < screen.height );
    }
    return r;
}
流殇 2025-01-04 10:53:29

要测试本机“orientationchange”支持,请在窗口中使用“orientationchange”

作为后备,使用 onresize 事件并检查 window.outerHeight > 。 window.outerWidth

To test native "orientationchange" support, use "orientationchange" in window

As a fallback, use onresize event and check window.outerHeight > window.outerWidth

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