webApp 用js 模仿微信的摇一摇功能DeviceMotionEvent

原创
2014/01/09 11:36
阅读数 792

webApp 用js 模仿微信的摇一摇功能

<script type="text/javascript">
var color = new Array('#fff', '#ff0', '#f00', '#000', '#00f', '#0ff');
if(window.DeviceMotionEvent) {
    var speed = 105;
    var x = y = z = lastX = lastY = lastZ = 0;
    
    
    var ys=1;
    window.addEventListener('devicemotion', function(){
        var acceleration =event.accelerationIncludingGravity;
        x = acceleration.x;
        y = acceleration.y;
        if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed) {
	        ys+=1;
	        alert("你摇一摇换了桌面哦")
        	document.getElementById("text").innerHTML+=ys+"<br />";
            document.body.style.backgroundColor = color[Math.round(Math.random()*10)%6];
        }
        lastX = x;
        lastY = y;
    }, false);
}
</script>



展开阅读全文
打赏
0
15 收藏
分享
加载中
更多评论
打赏
0 评论
15 收藏
0
分享
返回顶部
顶部