检测的Andr​​oid手机旋转与JavaScript的浏览器浏览器、手机、Andr、oid

2023-09-12 00:03:42 作者:陷入甜苦之间

我知道,在Safari浏览器在iPhone上,你可以通过侦听 onorientationchange 事件,并查询窗口检测屏幕的方位和方向的变化。定向的角度。

I know that in Safari on an iPhone you can detect the screen's orientation and change of orientation by listening for the onorientationchange event and querying window.orientation for the angle.

这是可能在Android手机上的浏览器?

Is this possible in the browser on Android phones?

需要明确的是,我问是否Android设备的旋转可以通过的JavaScript来检测标准的网页上运行。它可以在iPhone上,我不知道它是否能为Android手机来完成。

To be clear, I am asking whether the rotation of an Android device can be detected by JavaScript running on a standard web page. It is possible on an iPhone and I wondered whether it could be done for Android phones.

推荐答案

要检测的方向变化在Android浏览器,附加一个监听到 orientationchange 调整活动的窗口

To detect an orientation change on an Android browser, attach a listener to the orientationchange or resize event on window:

// 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的情况下)。

Check the window.orientation property to figure out which way the device is oriented. With Android phones, screen.width or screen.height also updates as the device is rotated. (this is not the case with the iPhone).