Internet Explorer 中的 JavaScript 方向更改问题

JavaScript orientation change issues in Internet Explorer

本文关键字:问题 方向 JavaScript Explorer 中的 Internet      更新时间:2023-09-26

每次移动设备从纵向切换到横向时,我都需要执行一些JavaScript逻辑。我使用以下检查来完成此操作:

var doOnOrientationChange = function() {
if (window.innerWidth > window.innerHeight) {
// Execute logic for users who are switching to landscape mode
}
};

这适用于所有iPhone和Android浏览器,但不适用于Windows Phone 8上的IE10。有时,当 IE10 用户点击输入时,会触发横向模式的逻辑。

为什么 IE10 将此操作视为用户切换到横向模式?

使用 IE10 resize事件而不是orientationchange。Windows Phone 8 中没有orientationchange事件。

j查询代码:

$(window).on('resize', function () {
    if (window.innerWidth > window.innerHeight) {
        // Execute logic for users who are switching to landscape mode
    }
});

另外,请阅读此答案以了解orientationchangeresize事件如何在Android和iOS设备中触发。