美文网首页
重力感应事件

重力感应事件

作者: 白鹤_ | 来源:发表于2017-09-27 10:11 被阅读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

}

相关文章

网友评论

      本文标题:重力感应事件

      本文链接:https://www.haomeiwen.com/subject/dhiyextx.html