手机摇一摇

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<img src="pic.jpg" id="imgLogo" alt="">
<p id="moCalcTiltLR"></p>
<p id="moCalcTiltFB"></p>
<script>
    function deviceMotionHandler(eventData) {
        // 
        var acceleration = eventData.accelerationIncludingGravity;
        var facingUp = -1;
        if (acceleration.z > 0) {
            facingUp = +1;
        }
        var tiltLR = Math.round(((acceleration.x) / 9.81) * -90);
        var tiltFB = Math.round(((acceleration.y + 9.81) / 9.81) * 90 * facingUp);

        document.getElementById("moCalcTiltLR").innerHTML = tiltLR;
        document.getElementById("moCalcTiltFB").innerHTML = tiltFB;
        var rotation = "rotate(" + tiltLR + "deg) rotate3d(1,0,0, " + (tiltFB) + "deg)";
        document.getElementById("imgLogo").style.webkitTransform = rotation;
    }
    // 
    if (window.DeviceMotionEvent) {
        window.addEventListener(devicemotion,deviceMotionHandler, false);
    }else{
        alert(亲,你的浏览器不支持DeviceMotionEvent哦~);
    }
</script>
</body>
</html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。