目录结构

引入jQuery:jquery-1.11.1.min.js

html代码

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>摇一摇功能</title>
    <script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="shake.js"></script>
    <script type="text/javascript">
     var count = 1;
     window.onload = function() { 
    var myShakeEvent = new Shake({ 
        threshold: 15 
    }); 
 
    myShakeEvent.start(); 
 
    window.addEventListener('shake', shakeEventDidOccur, false); 
 
    function shakeEventDidOccur () {
shakingAutoPlay();
$("#shakeMe").text("第"+count+"次摇晃手机");
count++;
window.setTimeout(shakeoverAutoPlay,1500);
    } 
}; 
 
//摇动时的声音
function shakingAutoPlay(){
var shaking = document.getElementById("shaking");//必须使用 document.getElementById 获取dom而非jQuery对象
shaking.loop = false;//必须设置为false   不然在android手机4.0版本上会一直循环播放该音效
shaking.play();
}
 
//摇动结束后的声音
function shakeoverAutoPlay(){
var shakeover = document.getElementById("shakeover");
shakeover.loop = false;
shakeover.play();
}
        
    </script>
</head>
<body>
<div id="pro"> 
    <p>使劲晃动您的手机</p>
    <h1 id="shakeMe">尚未摇动</h1> 
<audio id="shaking" src="shaking.mp3" controls="controls" loop="false" hidden="true"></audio>
<audio id="shakeover" src="shakeover.mp3" controls="controls" loop="false" hidden="true"></audio>
</div>
</body>
</html>

shake.js

/*
 * Author: Alex Gibson
 * https://github.com/alexgibson/shake.js
 * License: MIT license
 */
 
(function(global, factory) {
    if (typeof define === 'function' && define.amd) {
        define(function() {
            return factory(global, global.document);
        });
    } else if (typeof module !== 'undefined' && module.exports) {
        module.exports = factory(global, global.document);
    } else {
        global.Shake = factory(global, global.document);
    }
} (typeof window !== 'undefined' ? window : this, function (window, document) {
 
    'use strict';
 
    function Shake(options) {
        //feature detect
        this.hasDeviceMotion = 'ondevicemotion' in window;
 
        this.options = {
            threshold: 15, //default velocity threshold for shake to register
            timeout: 1000 //default interval between events
        };
 
        if (typeof options === 'object') {
            for (var i in options) {
                if (options.hasOwnProperty(i)) {
                    this.options[i] = options[i];
                }
            }
        }
 
        //use date to prevent multiple shakes firing
        this.lastTime = new Date();
 
        //accelerometer values
        this.lastX = null;
        this.lastY = null;
        this.lastZ = null;
 
        //create custom event
        if (typeof document.CustomEvent === 'function') {
            this.event = new document.CustomEvent('shake', {
                bubbles: true,
                cancelable: true
            });
        } else if (typeof document.createEvent === 'function') {
            this.event = document.createEvent('Event');
            this.event.initEvent('shake', true, true);
        } else {
            return false;
        }
    }
 
    //reset timer values
    Shake.prototype.reset = function () {
        this.lastTime = new Date();
        this.lastX = null;
        this.lastY = null;
        this.lastZ = null;
    };
 
    //start listening for devicemotion
    Shake.prototype.start = function () {
        this.reset();
        if (this.hasDeviceMotion) {
            window.addEventListener('devicemotion', this, false);
        }
    };
 
    //stop listening for devicemotion
    Shake.prototype.stop = function () {
        if (this.hasDeviceMotion) {
            window.removeEventListener('devicemotion', this, false);
        }
        this.reset();
    };
 
    //calculates if shake did occur
    Shake.prototype.devicemotion = function (e) {
        var current = e.accelerationIncludingGravity;
        var currentTime;
        var timeDifference;
        var deltaX = 0;
        var deltaY = 0;
        var deltaZ = 0;
 
        if ((this.lastX === null) && (this.lastY === null) && (this.lastZ === null)) {
            this.lastX = current.x;
            this.lastY = current.y;
            this.lastZ = current.z;
            return;
        }
 
        deltaX = Math.abs(this.lastX - current.x);
        deltaY = Math.abs(this.lastY - current.y);
        deltaZ = Math.abs(this.lastZ - current.z);
 
        if (((deltaX > this.options.threshold) && (deltaY > this.options.threshold)) || ((deltaX > this.options.threshold) && (deltaZ > this.options.threshold)) || ((deltaY > this.options.threshold) && (deltaZ > this.options.threshold))) {
            //calculate time in milliseconds since last shake registered
            currentTime = new Date();
            timeDifference = currentTime.getTime() - this.lastTime.getTime();
 
            if (timeDifference > this.options.timeout) {
                window.dispatchEvent(this.event);
                this.lastTime = new Date();
            }
        }
 
        this.lastX = current.x;
        this.lastY = current.y;
        this.lastZ = current.z;
 
    };
 
    //event handler
    Shake.prototype.handleEvent = function (e) {
        if (typeof (this[e.type]) === 'function') {
            return this[e.type](e);
        }
    };
 
    return Shake;
}));
 
 demo地址:http://files.cnblogs.com/files/avivaye/shake.zip

[HTML5] 手机摇一摇实现的更多相关文章

  1. 用HTML5实现手机摇一摇的功能(转)

    在百度开发者大会上我介绍过HTML5另外一个重要特性就是DeviceOrientation,它将底层的方向传感器和运动传感器进行了高级封装,提供了DOM事件的支持.这个特性包括两种事件: 1.devi ...

  2. Adobe Edge Animate --使用HTML5实现手机摇一摇功能

    Adobe Edge Animate --使用HTML5实现手机摇一摇功能 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. HTML5的发展日新月异,其功能 ...

  3. 【HTML5 】手机重力与方向感应的应用——摇一摇效果

    http://www.helloweba.com/view-blog-287.html HTML5有一个重要特性:DeviceOrientation,它将底层的方向和运动传感器进行了高级封装,它使我们 ...

  4. h5手机摇一摇功能实现:基于html5重力感应DeviceMotionEvent事件监听手机摇晃

    DeviceMotionEven是html5提供的一个用来获取设备物理方向及运动的信息(比如陀螺仪.罗盘及加速计)的Dom事件,事件描述如下: deviceorientation:提供设备的物理方向信 ...

  5. 利用HTML5的一个重要特性 —— DeviceOrientation来实现手机网站上的摇一摇功能

      介绍之前做两个声明: 以下代码可以直接运行,当然你别忘了引用jQuery才行. <script> // DeviceOrientation将底层的方向传感器和运动传感器进行了高级封装, ...

  6. 手机摇一摇效果-html5

    1.手机摇一摇效果实现 2.播放声音 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  7. html5 DeviceOrientation来实现手机网站上的摇一摇功能

    原文地址:http://www.cootm.com/?p=706 从网上转载看到的,感觉不错,就转过来了,特此感谢 cnblogs 的 幸福2胖纸的码农生活,直接转载了,不要介意!呵呵 以下是转载内容 ...

  8. 利用HTML5的devicemotion事件实现手机摇一摇抽奖,年会抽奖

    摇一摇JS脚本逻辑:接下来是移动端JS脚本逻辑的实现,摇一摇的实现需借助html5新增的devicemotion事件,获取设备在位置和方向上的改变速度的相关信息,该事件的基本使用如下: if (win ...

  9. html5实现微信摇一摇功能

    在HTML5中,DeviceOrientation特性所提供的DeviceMotion事件封装了设备的运动传感器时间,通过改时间可以获取设备的运动状态.加速度等数据(另还有deviceOrientat ...

随机推荐

  1. uitabbar 标题设置 button text attributes only respected for UIControlStateNormal

    uitabbar 标题设置 button text attributes only respected for UIControlStateNormal [[UITabBarItem appearan ...

  2. 算法笔记_151:算法提高 01背包(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 给定N个物品,每个物品有一个重量W和一个价值V.你有一个能装M重量的背包.问怎么装使得所装价值最大.每个物品只有一个. 输入格式 输入的第 ...

  3. PotPlayer 如何设置多屏幕全屏播放

      如何在播放器中,设置扩展播放模式? 全屏设置/主全屏显示设备:Display2   如何使视频播放时,没有黑边并且全屏充满? 高宽比/处理方式:保持全屏宽高比   如果取消掉视频上方的文字提示信息 ...

  4. jquery remove()不兼容问题解决方案

      jquery remove()不兼容问题解决方案 CreationTime--2018年7月27日10点19分 Author:Marydon 1.情景展示 点击关闭,将这个div移除掉 源码展示 ...

  5. ServletConfig讲解

    1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数. 例如: <ser ...

  6. javascript 的继承实例

    shape.prototype = { getEdge:function(){ return this.edge; }, getArea:function(){ return this.a*this. ...

  7. java web 中的MVC

    M:相当于Bean V:jsp C:servlet 当客户端发来请求,servlet响应请求处理请求,并把要发送给客户端的数据封装到Bean中,然后通过转发,将这个封装好了数据Bean送给jsp,js ...

  8. 在linux下新增一块硬盘的操作。(包含大于2T的硬盘在linux下挂载操作)

    转自:http://blog.csdn.net/season_hangzhou/article/details/36423223 一.安装硬盘到物理机上. 二.查看硬盘是否正确安装. 使用“fdisk ...

  9. VC版DoEvents

    VB和C#下有一个DoEvents方法,可以让程序在执行操作的同时仍可以处理其他事件.由于近期在做一个数据格式转换的项目,需要进行大批量的数据处理,希望能在进行数据读写过程中,程序还能接收其他操作,防 ...

  10. Android7.0 MessageQueue

    Android中的消息处理机制大量依赖于Handler.每一个Handler都有相应的Looper,用于不断地从相应的MessageQueue中取出消息处理. 一直以来,觉得MessageQueue应 ...