从设备方向获取水平标题

时间:2013-05-16 09:46:50

标签: javascript jquery orientation device-orientation heading

我想使用设备定位事件来检查移动设备的标题。 这就是我的工作:

if (window.DeviceOrientationEvent) {
    window.addEventListener("deviceorientation", handleOrientation, false);
}
else{
    alert("Device Orientation is not available");
}

在我的handleOrientation函数中,我可以使用alpha,beta,gamma。但我希望拥有该设备的标题。当您放下设备并执行以下操作时:

enter image description here

function handleOrientation(orientData) {

    //var absolute = orientData.absolute;
    //var alpha = orientData.alpha;
    //var beta = orientData.beta;
    //var gamma = orientData.gamma;
}

如何从上面的值中获取设备的标题?

尼尔斯

1 个答案:

答案 0 :(得分:1)

您可以使用window.orientation,但请注意:不同的设备会返回不同的值。

function handleOrientation() {

    switch(window.orientation)
    case 0:
     // do your stuff
     break;
    case -90
       ....
}

window.addEventListener('onorientationchange', handleOrientation);

iPhone有:(来自https://developer.apple.com/library/safari/#documentation/DataManagement/Reference/DOMWindowAdditionsReference/DOMWindowAdditions/DOMWindowAdditions.html

0 =>纵向。这是默认值。

-90 =>屏幕顺时针旋转时的横向方向。

90 =>逆时针旋转屏幕时的横向方向。

180 =>屏幕纵向翻转时的纵向方向。 iPhone目前不支持此值。