首頁 > web前端 > H5教程 > 主體

詳解html5如何取得手機陀螺儀角度資訊的範例程式碼

黄舟
發布: 2017-03-20 15:18:55
原創
3906 人瀏覽過

html5事件中,deviceorientation事件,此事件是偵測裝置方向變化時的事件。其常用屬性為alpha(x)、beta(y)、gamma(z)。

預設將手機垂直,且正面(90度)衝著自己

alpha:左右旋轉

beta:前後旋轉

gamma:扭轉(自轉)

<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>
    <p id="arrow"></p>
</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>
登入後複製

以上是詳解html5如何取得手機陀螺儀角度資訊的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!