- // 运用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;
- }
- function yaoAfter(){
- //do something
- }
- //说明:说见案例摇一摇效果中yao.js