shake.js实现微信摇一摇功能
项目要求实现点击摇一摇图片,图片摇一摇,并且摇一摇手机,图片也要摇一摇。
关于用js怎样实现摇一摇手机图片摇一摇,我在网络上找了一些方法,真正有用的是shake.js。
接下来,上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;
}));
我的项目是vue+webpack实现的,所以实现代码如下:
//引入shake.js
var Shake = require('./js/shake.js'); //如何使用
//手机摇一摇触发事件
//create a new instance of shake.js.
var myShakeEvent = new Shake({
threshold: 15
});
// start listening to device motion
myShakeEvent.start();
// register a shake event
window.addEventListener('shake', shakeEventDidOccur, false);
//shake event callback
function shakeEventDidOccur () {
//put your own code here etc.
$('.shake').addClass('shake_animate');
}
兼容性:
- iOS Safari 4.2.1 (and above)
- Android 4.0.3 (default browser)
- Chrome 41+ for Android
- Opera Mobile (Android)
- BlackBerry PlayBook 2.0
- Firefox for Android
- FirefoxOS Devices
shake.js在github路径:https://github.com/alexgibson/shake.js
shake.js实现微信摇一摇功能的更多相关文章
- js微信摇一摇功能以及api
一.摇一摇功能 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- C#开发微信门户及应用(38)--微信摇一摇红包功能
摇一摇周边红包接口是为线下商户提供的发红包功能.用户可以在商家门店等线下场所通过摇一摇周边领取商家发放的红包.我曾经在<C#开发微信门户及应用(28)--微信“摇一摇·周边”功能的使用和接口的实 ...
- 使用shake.js让你博客支持摇一摇
大家好,又到了随机文章的时间,请使用手机打开演示站点,然后像摇妹子一样摇晃手机,你会发现非常牛逼的事情,炫酷吧.该功能已经集成在Oconnor1.8中.本文主要讲解这货的原理. 首先需要下载shake ...
- C#开发微信门户及应用(28)--微信“摇一摇·周边”功能的使用和接口的实现
”摇一摇周边“是微信提供的一种新的基于位置的连接方式.用户通过“摇一摇”的“周边”页卡,可以与线下商户进行互动,获得商户提供的个性化的服务.微信4月份有一个赠送摇一摇设备的活动,我们有幸获得赠送资格, ...
- IOS开发之——类似微信摇一摇的功能实现
首先,一直以为摇一摇的功能实现好高大上,结果百度了.我自己也模仿写了一个demo.主要代码如下: 新建一个项目,名字为AnimationShake. 主要代码: - (void)motionBegan ...
- Android仿iPhone晃动撤销输入功能(微信摇一摇功能)
重力传感器微信摇一摇SensorMannager自定义alertdialogSensorEventListener 很多程序中我们可能会输入长文本内容,比如短信,写便笺等,如果想一次性撤销所有的键入内 ...
- js html5 仿微信摇一摇
看微信摇一摇之后忽然想知道他是怎么写的.于是就在网上查了一些资料,有些是借鉴别人的.大家共同学习啊 先放代码 <body onload="init()"> <p& ...
- H5+JS实现手机摇一摇功能
在做微信活动页面的时候,经常会需要实现手机摇一摇功能,比如“摇一摇,拿好礼”. 为了实现它,就需要用到HTML5的DeviceOrientation特性.它提供的DeviceMotion事件封装了设备 ...
- html5实现微信摇一摇功能
在HTML5中,DeviceOrientation特性所提供的DeviceMotion事件封装了设备的运动传感器时间,通过改时间可以获取设备的运动状态.加速度等数据(另还有deviceOrientat ...
随机推荐
- wan口的ip是干什么用的
wan口的ip是外网的ip,属于公网的ip.主要用于外网的识别,WAN口主要用于连接外部网络,如ADSL.DDN.以太网等各种接入线路:而LAN口用来连接家庭内部网络,主要与家庭网络中的交换机.集线器 ...
- CISCN 2019 writeup
划水做了两个pwn和两个逆向...... 二进制题目备份 Re easyGO Go语言,输入有Please字样,ida搜索sequence of bytes搜please的hex值找到字符串变量,交叉 ...
- linux 测试磁盘iops 方法详解
一.FIO安装 wget http://brick.kernel.dk/snaps/fio-2.0.7.tar.gz yum -y install libaio-devel tar -zxvf ...
- 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_08-新增页面-前端-Api调用
表单数据提交到后台 export const page_add = paramas => { return http.requestPost(apiUrl+'/cms/page/add',par ...
- C# 实现启用或停止本地网络连接
获取本机网络连接列表 public static List<string> GetNetList() { List<string> ls = new List<strin ...
- CSRF利用
使用burpsuite的csrf poc选项,可以生成HTML代码 json CSRF flash + 307跳转 https://github.com/sp1d3r/swf_json_csrf
- leetcode654 Maximum Binary Tree
思路: 使用单调栈可以达到O(n). 实现: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tre ...
- SQL FIND_IN_SET() 判断某一个数是否存在于数据表某个以逗号分隔开字段数据中
数据表中的字段存储的是以逗号分隔开的字符串, 例如 (1,2,6,8) 以前不知道这个用法, 碰到比如 8 是否包含在改字符串里面只能一个个取出来, 然后解析成数组,再判断是否在该数组中,效率极低: ...
- spring cloud微服务docker启动
1. 背景 将系统拆分成微服务,一个系统可能拆分成十几个服务,服务太多会影响发布和管理,所以容器话之后易于管理和版本发布,鉴于此将spring cloud微服无docker容器化. 2.依赖环境 Ce ...
- 【AtCoder】AGC002
AGC002 A - Range Product #include <bits/stdc++.h> #define fi first #define se second #define p ...