代码如下:
var count = 0;
function shakeFun(){ //判断手机摇晃 if(window.DeviceMotionEvent) { var speed = 10; var x = y = z = lastX = lastY = lastZ = 0; window.addEventListener('devicemotion', function(){ var acceleration =event.accelerationIncludingGravity; x = acceleration.x; y = acceleration.y; if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed) { count++; if(count == 1 ){ //避免摇晃中多次触发事件 function shake(){ //摇晃时展示的动效 $(".center_phone").css("transform","rotate(16deg)"); setTimeout(function(){ $(".center_phone").css("transform","rotate(-16deg)"); },200); var audio = document.getElementById('audio');//获取audio元素 audio.play(); }; var timer = setInterval(shake,500);//每隔1秒执行一次 setTimeout(function(){ clearInterval(timer); },3000); setTimeout(function(){ //摇晃后触发事件
count = 0; showDia(); },4000); } } lastX = x; lastY = y; }, false); } };
标签:function,count,h5,摇晃,var,手机,audio,speed From: https://www.cnblogs.com/redFeather/p/16917592.html