手机摇一摇监听

最近项目需要用到手机摇一摇,之前没做过,就在网上找了一下,在此记录一下,直接上代码。

// 手机摇一摇监听
                (function(global, factory) {
                    $('.p6-tishi').hide();
                    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, //默认摇动阈值
                            timeout: 1000 //默认两次事件间隔时间
                        };
                        if (typeof options === 'object') {
                            for (var i in options) {
                                if (options.hasOwnProperty(i)) {
                                    this.options[i] = options[i];
                                }
                            }
                        }
                        //使用date防止重复调用
                        this.lastTime = new Date();
                        //accelerometer values
                        this.lastX = null;
                        this.lastY = null;
                        this.lastZ = null;
                        //创建自定义事件
                        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;
                        }
                    }
                    //重置时间计时器
                    Shake.prototype.reset = function () {
                        this.lastTime = new Date();
                        this.lastX = null;
                        this.lastY = null;
                        this.lastZ = null;
                    };
                    //开始监听 devicemotion
                    Shake.prototype.start = function () {
                        this.reset();
                        if (this.hasDeviceMotion) {
                            window.addEventListener('devicemotion', this, false);
                        }
                    };
                    //停止监听 devicemotion
                    Shake.prototype.stop = function () {
                        if (this.hasDeviceMotion) {
                            window.removeEventListener('devicemotion', this, false);
                        }
                        this.reset();
                    };
                    //计算是否触发摇动
                    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))) {
                            //计算上次触发摇动到现在的间隔时间
                            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;
                    };
                    //事件处理
                    Shake.prototype.handleEvent = function (e) {
                        if (typeof (this[e.type]) === 'function') {
                            return this[e.type](e);
                        }
                    };
                    return Shake;
                }));
                //创建实例
                var myShakeEvent = new Shake({
                    threshold: 4, // 摇动阈值
                    timeout: 500 // 事件发生频率,是可选值
                });
                // 监听设备动作
                myShakeEvent.start();

                //添加一个监听事件
                window.addEventListener('shake', shakeEventDidOccur, false);
                //摇动回调函数
                function shakeEventDidOccur() {
                    //摇动后执行的动作
                    // 获取当前页面的page数
                    var cp = app.currentPage;
                    if(cp == 6) {
                        if(app.flage) {
                            app.flage = false;
                            app.showPage(7);
                            $('.p8').delay(500).show(0);
                            app.pages[8].init();
                        }
                    }
                }

实例以上的代码可以封装在一个文件里。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的手机摇一摇抽奖代码实现示例: HTML部分: ```html <!-- 抽奖按钮 --> <button id="lotteryBtn">摇一摇抽奖</button> <!-- 中奖提示 --> <div id="result"></div> ``` JavaScript部分: ```javascript // 监听DeviceMotion事件 window.addEventListener('devicemotion', function(event) { // 获取加速度数据 var acceleration = event.accelerationIncludingGravity; // 计算加速度大小 var accelerationMagnitude = Math.sqrt( Math.pow(acceleration.x, 2) + Math.pow(acceleration.y, 2) + Math.pow(acceleration.z, 2) ); // 判断是否达到摇一摇条件 if (accelerationMagnitude > 20) { // 触发抽奖事件 lottery(); } }); // 抽奖函数 function lottery() { // 随机生成中奖结果(这里假设有10%的概率中奖) var isWin = Math.random() < 0.1; if (isWin) { // 中奖提示 document.getElementById('result').innerHTML = '恭喜你中奖了!'; } else { // 未中奖提示 document.getElementById('result').innerHTML = '很遗憾,未中奖!'; } } // 绑定抽奖按钮点击事件 document.getElementById('lotteryBtn').addEventListener('click', function() { // 触发抽奖事件 lottery(); }); ``` 在实现中,我们通过监听DeviceMotion事件来判断手机是否摇动,如果达到摇一摇条件,就触发抽奖事件。在抽奖事件中,我们通过随机生成中奖结果来模拟抽奖过程,并根据结果提示用户是否中奖。同时,我们也通过绑定抽奖按钮点击事件来实现手动抽奖的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值