码迷,mamicode.com
首页 > 移动开发 > 详细

手机摇一摇

时间:2015-05-14 16:07:32      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

<!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>

 

手机摇一摇

标签:

原文地址:http://www.cnblogs.com/light-j/p/4503203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!