码迷,mamicode.com
首页 > 移动开发 > 详细

JS 手机摇动

时间:2014-07-23 12:51:46      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   re   c   

var SHAKE_THRESHOLD = 800;//摇动强度
var lastUpdate = 0;
var x, y, z, last_x, last_y, last_z;
var canshake = false;
function deviceMotionHandler(eventData) {
    var acceleration = eventData.accelerationIncludingGravity;
    var curTime = new Date().getTime();
    if ((curTime - lastUpdate) > 100) {
        var diffTime = (curTime - lastUpdate);
        lastUpdate = curTime;
        x = acceleration.x;
        y = acceleration.y;
        z = acceleration.z;
        var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
        if (canshake && speed > SHAKE_THRESHOLD) {
            canshake = false;
            //DO STH
       if (window.DeviceMotionEvent) { window.removeEventListener(‘devicemotion‘, deviceMotionHandler, false); }
        }
        last_x = x;
        last_y = y;
        last_z = z;
    }
}

JS 手机摇动,布布扣,bubuko.com

JS 手机摇动

标签:style   blog   color   io   re   c   

原文地址:http://www.cnblogs.com/allvie/p/3862571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!