本文介绍了使用Javascript检测iOS Safari中的抖动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用新的MobileSafari设备运动API捕获摇动事件?
How can I utilize the new MobileSafari device motion APIs to capture a "shake" event?
推荐答案
查看此真棒博客发布:
See this awesome blog post: http://www.jeffreyharrell.com/blog/2010/11/creating-a-shake-event-in-mobile-safari/
其中说明了此示例:
if (typeof window.DeviceMotionEvent != 'undefined') {
// Shake sensitivity (a lower number is more)
var sensitivity = 20;
// Position variables
var x1 = 0, y1 = 0, z1 = 0, x2 = 0, y2 = 0, z2 = 0;
// Listen to motion events and update the position
window.addEventListener('devicemotion', function (e) {
x1 = e.accelerationIncludingGravity.x;
y1 = e.accelerationIncludingGravity.y;
z1 = e.accelerationIncludingGravity.z;
}, false);
// Periodically check the position and fire
// if the change is greater than the sensitivity
setInterval(function () {
var change = Math.abs(x1-x2+y1-y2+z1-z2);
if (change > sensitivity) {
alert('Shake!');
}
// Update new position
x2 = x1;
y2 = y1;
z2 = z1;
}, 150);
}
这篇关于使用Javascript检测iOS Safari中的抖动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!