javascript - 音隙方向变化

标签 javascript android

window.onresize = function(){ updateOrientation(e); }
    function updateOrientation(e) {
        alert("hey");

        deviceWidth = $('body').width();
        if (deviceWidth == 320) {
            currentOrientation = "portrait";
        }
        else if (deviceWidth == 480) {
            currentOrientation = "landscape";
        }

        // fire a function that checks the orientation every x milliseconds
        setInterval(checkOrientation, 500);

        // check orientation
        function checkOrientation() {
            deviceWidth = $('body').width();
            if (deviceWidth >= '1200') {
                newOrientation = "portrait";
            }
            else if (deviceWidth <= '900') {
                newOrientation = "landscape";
            }
            // if orientation changed since last check, fire either the portrait or landscape function
            if (newOrientation != currentOrientation) {
                if (newOrientation == "portrait") {
                    changedToPortrait();
                }
                else if (newOrientation == "landscape") {
                    changedToLandscape();
                }
                currentOrientation = newOrientation;
            }
        }

        // landscape stuff
        function changedToLandscape() {
            alert('Orientation has changed to Landscape!');
        }

        // portrait stuff
        function changedToPortrait() {
            alert('Orientation has changed to Portrait!');
        }
    }

我正在使用它来使用phonegap检测Android平板电脑中的方向变化。不知怎的,代码不起作用。有什么建议么?我猜测它无法生成方向改变事件。

最佳答案

这对我在 PhoneGap 中有用:

function onOrientationChange() {
    switch (window.orientation) {
        case -90:
        case 90:
            alert('landscape');
            break;
        default:
            alert('portrait');
            break;
    }
};
window.onresize = onOrientationChange;

关于javascript - 音隙方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7803578/

相关文章:

javascript - 在不同选项卡中打开 react 相对路径组件会破坏页面

javascript - Play 2 框架 + Loop for + 动态Javascript

javascript - $.ajax 无法正常工作

javascript - 如何通过 JavaScript 解析和更改实时网站上 CSS 样式表的颜色?

Javascript (D3) 循环优化

java - 可以将字符串编译为代码吗?

java - Android 中的左右滑动 webview

java - Eclipse logcat 调试

Android EditText onKeyUp

android - 在 android 模拟器上运行 youtube 视频