重力感应事件

2017-09-27  本文已影响0人  白鹤_

// 运用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

}

上一篇下一篇

猜你喜欢

热点阅读