The Web page of the mobile phone shake effect, JS code as follows
var speed;//to determine if the phone has a shake function if (window. Devicemotionevent) {//Bind Devicemotion event Window.addeventlistener (' Devicemotion ', Devicemotionhandler, false);} else{alert (' Your device does not support shake-shake function, the system will automatically shake your phone ^_^ '); Speed = 888; Do not support shake when shaking, modify speed value trigger Shake Phone event}//initialize var shake_threshold = 800;var last_update = 0;var x, y, Z, last_x, last_y, last_z;function Devicemotionhandler (eventData) {var acceleration =eventdata.accelerationincludinggravity; var curtime = new Date (). GetTime (); if ((curtime-last_update) >) {var difftime = curtime-last_update; Last_update = Curtime; x = acceleration.x; y = acceleration.y; z = acceleration.z; Speed = Math.Abs (x +y + z-last_x-last_y-last_z)/difftime * 10000; Speed = 888; if (Speed > Shake_threshold) {alert ("shaked!") when the velocity is higher than the threshold value; } last_x = x; last_y = y; Last_z = Z; }}
Mobile phone-shaking function code implementation