15

我正在编写一个针对 iPhone 的网站。<body>当 iPhone 的方向改变时(即当用户将手机变成横向和/或纵向模式时),我想在元素上设置一个类。

我可以通过 JavaScript 检测到这种变化吗?有这方面的活动吗?

4

2 回答 2

26

是的,通过onorientationchange事件和window.orientation属性。

(在 iOS 以外的平台上,改为使用ScreenOrientationAPI。)

于 2009-05-11T23:06:46.780 回答
1

您可以检查文档正文宽度。如果它突然变小或变大,你就知道发生了什么。你可以用一个间隔来测量它。

window.setInterval(function() {
    // check body with here and compare with global variable that holds the last measurement
    // you may set a boolean isLandscape = true if the body with became bigger.
}, 50);
于 2009-05-11T23:25:35.203 回答