我正在编写一个针对 iPhone 的网站。<body>
当 iPhone 的方向改变时(即当用户将手机变成横向和/或纵向模式时),我想在元素上设置一个类。
我可以通过 JavaScript 检测到这种变化吗?有这方面的活动吗?
我正在编写一个针对 iPhone 的网站。<body>
当 iPhone 的方向改变时(即当用户将手机变成横向和/或纵向模式时),我想在元素上设置一个类。
我可以通过 JavaScript 检测到这种变化吗?有这方面的活动吗?
是的,通过onorientationchange
事件和window.orientation
属性。
(在 iOS 以外的平台上,改为使用ScreenOrientation
API。)
您可以检查文档正文宽度。如果它突然变小或变大,你就知道发生了什么。你可以用一个间隔来测量它。
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);