h5重力感应

手机中的方位轴在Web应用中调用手机陀螺仪接口//摇一摇(使用DeviceOrientation事件, 本质是计算偏转角)//测试中发现有些设备不支持if(window.DeviceOrientationEvent){$(window).on('deviceorientation',function(e) {if(isStarted) {returntrue;}if(!lastAcc) {lastAcc = e;returntrue;}vardelA = Math.abs(e.alpha - lastAcc.alpha);vardelB = Math.abs(e.beta - lastAcc.beta);vardelG = Math.abs(e.gamma - lastAcc.gamma);if( (delA > 15 && delB > 15) || (delA > 15 && delG > 15) || (delB > 15 || delG > 15)) {start();}lastAcc = e;});//摇一摇(使用DeviceMotion事件, 推荐,应为可以计算加速度)if(window.DeviceMotionEvent) {varspeed = 25;varx, y, z, lastX, lastY, lastZ;x = y = z = lastX = lastY = lastZ = 0;window.addEventListener('devicemotion',function(event){varacceleration = event.accelerationIncludingGravity;x = acceleration.x;y = acceleration.y;if(Math.abs(x-lastX) > speed || Math.abs(y-lastY) > speed) {start();}lastX = x;lastY = y;}, false);}摇一摇的代码判断逻辑varisStarted = false;// 开始摇签functionstart() {isStarted = true;$('.qiancover').hide();$('.decode').hide();$('.result').show();// setTimeout(showDecode, 3000);}// 显示正在解签functionshowDecode() {$('.result').hide();$('.decode').show();setTimeout(jumpToDecode, 3000);}// 跳至签文页面functionjumpToDecode(){varurls = ["#","#"];varjumpTo = urls[parseInt(Math.random() * urls.length)];window.location = jumpTo;};

猜你喜欢

转载自www.cnblogs.com/mapsxy/p/10280545.html
H5