重力感应事件

// 运用HTML5的deviceMotion,调用重力感应事件

if(window.DeviceMotionEvent){

document.addEventListener('devicemotion', deviceMotionHandler,false)

}

var  speed =30;

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 ){

//这里是摇动后要执行的方法yaoAfter();

}

lastX = x;

lastY = y;

lastZ = z;

}functionyaoAfter(){

//do something

}

你可能感兴趣的:(重力感应事件)