HTML5实现摇一摇

var isyao=false;//控制强烈摇动,连续触发事件
        //手机摇动事件
        _self.ShakeMobileEven=function(){
            if (window.DeviceMotionEvent) {                
                window.addEventListener('devicemotion', deviceMotionHandler, false);                                
            } else {
                alertShow('本设备不支持devicemotion事件');
            }
            var speed = 23;
            var x = y = z = lastX = lastY = lastZ = 0;
            function deviceMotionHandler(event) {
                if(isyao==true){
                    return;
                }
                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) {
                    isyao=true;
                    //摇动成功执行的代码在此处
                }
                lastX = x;
                lastY = y;
                lastZ = z;
            }           
        }

  另外,更多摇动实例参考:http://download.csdn.net/detail/yinluhui/8944075

你可能感兴趣的:(HTML5实现摇一摇)