分享网页检测摇一摇实例代码
作者:bea
废话不多说了,直接给大家贴代码了,具体代码如下所示: var Shaker = function(f){// 摇一摇: 检测到3次摇动算一次摇一摇, 摇动后调用处理函数, 不再检测摇动// f 摇动后的回调this.callback = f;this.status = 0; // 0: 侦听未开始 1: 侦听开始 this.speed = 15;this.lastX = this.lastY = this.lastZ = 0;this.num = 0; // 检测触发次数
废话不多说了,直接给大家贴代码了,具体代码如下所示:
var Shaker = function(f){
// 摇一摇: 检测到3次摇动算一次摇一摇, 摇动后调用处理函数, 不再检测摇动
// f 摇动后的回调
this.callback = f;
this.status = 0; // 0: 侦听未开始 1: 侦听开始
this.speed = 15;
this.lastX = this.lastY = this.lastZ = 0;
this.num = 0; // 检测触发次数
this.minNum = 3; // 最小检测触发次数
this.beginSecond = 0; // 开始检测的秒数
this.maxSecond = 3; // 最大间隔秒数
this.handlerWrap = function(){};
}
Shaker.prototype.listen = function(){
// 侦听摇动
var that = this;
if (this.status == 0 && window.DeviceMotionEvent) {
this.status = 1;
this.handlerWrap = function(event){
that.handler(event)
}
window.addEventListener('devicemotion', this.handlerWrap, false);
}
}
Shaker.prototype.release = function(){
// 停止侦听
if(this.status == 1){
if (window.DeviceMotionEvent) {
window.removeEventListener('devicemotion', this.handlerWrap);
}
this.status = 0;
this.num = 0;
}
}
Shaker.prototype.reset = function(){
// 重置检测
if(this.status == 1){
this.num = 0;
}
}
Shaker.prototype.handler = function(event){
// 传感器事件处理
if(this.status == 1){
var acceleration =event.accelerationIncludingGravity;
var x = acceleration.x;
var y = acceleration.y;
var z = acceleration.z;
if( Math.abs(x-this.lastX) > this.speed ||
Math.abs(y-this.lastY) > this.speed ||
Math.abs(z-this.lastZ) > this.speed )
{
if(this.num == 0){
this.beginSecond = Date.parse(new Date()) / 1000
}
this.num += 1;
}
this.lastX = x;
this.lastY = y;
this.lastZ = z;
if(this.num >= this.minNum){
var now = Date.parse(new Date()) / 1000;
if(now - this.beginSecond <= this.maxSecond){
this.release();
if(this.callback){
this.callback();
}
}
this.reset();
}
}
}
用法:
var s = new Shaker(function(){ /*摇动后的回调*/ });
s.listen();
好了,本文所述给大家介绍到这里,后续还会持续更新,希望本文给大家分享网页检测摇一摇实例代码的相关知识对大家有所帮助。
有用 | 无用
var Shaker = function(f){
// 摇一摇: 检测到3次摇动算一次摇一摇, 摇动后调用处理函数, 不再检测摇动
// f 摇动后的回调
this.callback = f;
this.status = 0; // 0: 侦听未开始 1: 侦听开始
this.speed = 15;
this.lastX = this.lastY = this.lastZ = 0;
this.num = 0; // 检测触发次数
this.minNum = 3; // 最小检测触发次数
this.beginSecond = 0; // 开始检测的秒数
this.maxSecond = 3; // 最大间隔秒数
this.handlerWrap = function(){};
}
Shaker.prototype.listen = function(){
// 侦听摇动
var that = this;
if (this.status == 0 && window.DeviceMotionEvent) {
this.status = 1;
this.handlerWrap = function(event){
that.handler(event)
}
window.addEventListener('devicemotion', this.handlerWrap, false);
}
}
Shaker.prototype.release = function(){
// 停止侦听
if(this.status == 1){
if (window.DeviceMotionEvent) {
window.removeEventListener('devicemotion', this.handlerWrap);
}
this.status = 0;
this.num = 0;
}
}
Shaker.prototype.reset = function(){
// 重置检测
if(this.status == 1){
this.num = 0;
}
}
Shaker.prototype.handler = function(event){
// 传感器事件处理
if(this.status == 1){
var acceleration =event.accelerationIncludingGravity;
var x = acceleration.x;
var y = acceleration.y;
var z = acceleration.z;
if( Math.abs(x-this.lastX) > this.speed ||
Math.abs(y-this.lastY) > this.speed ||
Math.abs(z-this.lastZ) > this.speed )
{
if(this.num == 0){
this.beginSecond = Date.parse(new Date()) / 1000
}
this.num += 1;
}
this.lastX = x;
this.lastY = y;
this.lastZ = z;
if(this.num >= this.minNum){
var now = Date.parse(new Date()) / 1000;
if(now - this.beginSecond <= this.maxSecond){
this.release();
if(this.callback){
this.callback();
}
}
this.reset();
}
}
}
用法:
var s = new Shaker(function(){ /*摇动后的回调*/ });
s.listen();
好了,本文所述给大家介绍到这里,后续还会持续更新,希望本文给大家分享网页检测摇一摇实例代码的相关知识对大家有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JQuery实现网页右侧随动广告特效
- Validform+layer实现漂亮的表单验证特效
- javascript实现倒计时跳转页面
- jQuery实现图片走马灯效果的原理分析
- JavaScript时间操作之年月日星期级联操作
- input点击后placeholder中的提示消息消失
- jQuery插件imgPreviewQs实现上传图片预览
- 实例详解AngularJS实现无限级联动菜单
- 利用CSS3在Angular中实现动画
- JavaScript程序开发之JS代码放置的位置
- 探讨JavaScript标签位置的存放与功能有无关系
- JavaScript知识点总结之如何提高性能
- jQuery动态添加及删除表单上传元素的方法(附demo源码下载)
- JavaScript焦点事件、鼠标事件和滚轮事件使用详解
- JavaScript提高性能知识点汇总
- 学习JavaScript设计模式之中介者模式
- 轻松实现jquery手风琴效果
- jQuery取得iframe中元素的常用方法详解
- js实现prototype扩展的方法(字符串,日期,数组扩展)