html5 移动设备旋转角度信息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tdjqqq/article/details/51444345

html5 提供的deviceorientation事件,可方便获取转角度信息。

注意:alpha,bate,gamma代表三轴,即x,y,z。

<html>
<head>
    <title>DeviceOrientationEvent</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <script src="js/common/jquery.min.js"></script>
    <script src="js/common/eventutil.js"></script>
</head>
<body>
    <div id="arrow"></div>
</body>
</html>

<script>
    try {
        var text = "";
        window.addEventListener("deviceorientation", orientationHandler, false);
        function orientationHandler(event) {
            text = ""
            var arrow = document.getElementById("arrow");
            text += "左右旋转:rotate alpha{" + Math.round(event.alpha) + "deg)<p>";
            text += "前后旋转:rotate beta{" + Math.round(event.beta) + "deg)<p>";
            text += "扭转设备:rotate gamma{" + Math.round(event.gamma) + "deg)<p>";
            arrow.innerHTML = text;
        }
    }
    catch (e) {
        $("#arrow").html(e.message)
    }
</script>


猜你喜欢

转载自blog.csdn.net/tdjqqq/article/details/51444345