首页 > 代码库 > 手机摇一摇进入某页面_js代码

手机摇一摇进入某页面_js代码

// 摇一摇
 var SHAKE_THRESHOLD = 1600;
    var last_update = 0;
    var x = y = z = last_x = last_y = last_z = 0;

    if (window.DeviceMotionEvent) {
        window.addEventListener(‘devicemotion‘, deviceMotionHandler, false);
    } else {
        alert(‘本设备不支持摇一摇事件‘);
    }

    function deviceMotionHandler(eventData) {
        var acceleration = eventData.accelerationIncludingGravity;
        var curTime = new Date().getTime();

        if ((curTime - last_update) > 100) {
            var diffTime = curTime - last_update;
            last_update = 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;
            var status = document.getElementById("status");

            if (speed > SHAKE_THRESHOLD) {
                openswt();
            }
            last_x = x;
            last_y = y;
            last_z = z;
        }
    }
   // function doResult() {
   //  window.location.href = "http://www.mamicode.com/tel:";}
   function openswt() {window.location.href = "http://www.mamicode.com/call.html";}
document.write(‘<div style="position:fixed;top:2%;right:0px;width:55px;z-index:10000;"><a href="javascript:getSwt(\‘yao\‘);"><img src="http://www.baidu.com/images/yao.gif" width="55" border="0"></a></div>‘);

 

手机摇一摇进入某页面_js代码