1

我正在尝试检测移动设备的加速度。我已经尝试过使用 Accelerometer,现在我也在尝试 deviceMotionEvent。每当我在 html 或控制台中打印属性 event.acceleration.x、.y 或 .z 时,我都会得到 null。如果有人能帮我看看我做错了什么,我将不胜感激。当我在浏览器中检查站点以查看我的设备是否可以使用加速度计或 deviceMotionEvent 时,它可以在那里工作,所以并不是我的设备无法处理它。这是我从 body 获得的许多谷歌搜索拼凑而成的迭代:

    <p id="test"> nothing </p>
    <script>
        function motion(event) {
            document.getElementById("test").innerHTML = "acceleration x:" + event.acceleration.x;

            console.log("Accelerometer: "
                + event.acceleration.x + ", "
                + event.acceleration.y + ", "
                + event.acceleration.z
            );
        }

        if (window.DeviceMotionEvent) {
            window.addEventListener("devicemotion", motion, false);
            document.getElementById("test").innerHTML = "eventlistner added"
        } else {
            document.getElementById("test").innerHTML = "sadness it won't work";
            console.log("DeviceMotionEvent is not supported");
        }
    </script>

文档上的文本最终是“加速度 x:null”。洞察力受到赞赏。谢谢!

4

0 回答 0