微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用Javascript检测iOS Safari中的抖动?

如何利用新的MobileSafari设备运动API捕获“摇”事件?

解决方法

看到这个真棒博客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);
}

原文地址:https://www.jb51.cc/js/155216.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐