標籤:false pac lis line func 代碼最佳化 eve height lin
首先對DeviceMotionEvent進行最佳化;
去除沒用的代碼,又一次封裝DeviceMotionEven
if(window.DeviceMotionEvent) {var speed = 25;//定義一個數值var x = y = z = lastX = lastY = lastZ = 0;//重設全部數值window.addEventListener(‘devicemotion‘, function(){var acceleration =event.accelerationIncludingGravity;//將感測值賦給acceleration x = acceleration.x;y = acceleration.y;z = acceleration.z;if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed ) {// TODO:在此處能夠實現搖一搖之後所要進行的資料邏輯操作 donghua();}lastX = x;lastY = y;lastZ = z;}, false);}
因為實際項目中有非常多需求無法非常好的實現。
比方:動畫不運行完成就不能繼續運行DeviceMotionEvent事件;
所以做了進一步最佳化;
var f=1;function donghua(){//動畫事件$(".img").animate({left:‘0‘,opacity:‘1‘},700,function(){f=1;});})。if(window.DeviceMotionEvent) {var speed = 25;//定義一個數值var x = y = z = lastX = lastY = lastZ = 0;//重設全部數值window.addEventListener(‘devicemotion‘, function(){var acceleration =event.accelerationIncludingGravity;//將感測值賦給acceleration x = acceleration.x;y = acceleration.y;z = acceleration.z;if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed ) {// TODO:在此處能夠實現搖一搖之後所要進行的資料邏輯操作 if(f==1){ donghua(); f=0;}}lastX = x;lastY = y;lastZ = z;}, false);}如今就完美了
html5搖一搖代碼最佳化