我知道在iPhone上的Safari中,你可以通过监听onorientationchange
事件和查询window.orientation
的Angular 来检测屏幕的方向和方向的变化.
在安卓手机的浏览器中,这可能吗?
明确地说,我想问的是,在一个标准网页上运行JavaScript次,是否可以检测到安卓设备的旋转.这在iPhone上是可能的,我想知道是否可以在Android手机上实现.
我知道在iPhone上的Safari中,你可以通过监听onorientationchange
事件和查询window.orientation
的Angular 来检测屏幕的方向和方向的变化.
在安卓手机的浏览器中,这可能吗?
明确地说,我想问的是,在一个标准网页上运行JavaScript次,是否可以检测到安卓设备的旋转.这在iPhone上是可能的,我想知道是否可以在Android手机上实现.
要检测Android浏览器上的方向更改,请将侦听器附加到window
上的orientationchange
或resize
事件:
// Detect whether device supports orientationchange event, otherwise fall back to
// the resize event.
var supportsOrientationChange = "onorientationchange" in window,
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
window.addEventListener(orientationEvent, function() {
alert('HOLY ROTATING SCREENS BATMAN:' + window.orientation + " " + screen.width);
}, false);
判断window.orientation
属性以确定设备的方向.对于Android手机,screen.width
或screen.height
也会随着设备的旋转而更新.(iPhone并非如此).