1

我正在使用 JQM 并且我使用的方向更改:

$(window).bind('orientationchange', handleOrientationChange);

function handleOrientationChange() {
       var viewportWidth = 0;
           var viewportHeight = 0;

           // calculate map height and width
           if( typeof( window.innerWidth ) == 'number' ) {
               viewportWidth = window.innerWidth;
               viewportHeight = window.innerHeight;
           } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
               viewportWidth = document.documentElement.clientWidth;
               viewportHeight = document.documentElement.clientHeight;
           } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
               viewportWidth = document.body.clientWidth;
               viewportHeight = document.body.clientHeight;
}

在 Android 中,当调用句柄函数时,我会在方向改变之前而不是之后获得尺寸。在 iPhone 上它工作正常。我需要添加一个 0.5/1 秒的小延迟......

有人知道原因或有更好的解决方案吗?

非常感谢。

4

1 回答 1

0

我已经setTimeout成功地使用了这个想法。只要确保一次只设置一个超时:

var oc_timer;
$(window).bind('orientationchange', function () {
    clearTimeout(oc_timer);
    oc_timer = setTimeout(handleOrientationChange, 500);
});

我认为问题在于orientationchange事件在 Android 设备上被触发得太快,并且事件触发时视口尺寸尚未更新。我已经尝试绑定到resize事件,但它也需要超时。

于 2012-07-31T22:47:06.563 回答