html5重力感应事件

 if (window.DeviceMotionEvent) {
                window.addEventListener('devicemotion',deviceMotionHandler, false);
            }
            var speed = 30;//speed
            var x = y = z = lastX = lastY = lastZ = 0;
            function deviceMotionHandler(eventData) {
                var acceleration =event.accelerationIncludingGravity;
                x = acceleration.x;
                y = acceleration.y;
                z = acceleration.z;
                if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed || Math.abs(z-lastZ) > speed) {
                    //简单的摇一摇触发代码
                    alert(1);
                }
                lastX = x;
                lastY = y;
                lastZ = z;
            }

摇一摇触发事件,感觉还是蛮高大上的

posted @ 2016-08-10 10:55  djdliu  阅读(265)  评论(0)    收藏  举报