jQuery触屏插件:Tap 代码

本文详细介绍了jQuery触屏插件Tap的使用方法及核心实现原理,包括事件监听、坐标计算和手势判断等关键步骤。

jQuery触屏插件:Tap,使用方法非常简单,例:
$("#domid").tap(function(){
alert("You tapped me! -- by"+this.innerText);
});
依赖jquery 1.7+

01
$.fn.tap = function(fn){
02
var collection = this,
03
isTouch = "ontouchend" in document.createElement("div"),
04
tstart = isTouch ? "touchstart" : "mousedown",
05
tmove = isTouch ? "touchmove" : "mousemove",
06
tend = isTouch ? "touchend" : "mouseup",
07
tcancel = isTouch ? "touchcancel" : "mouseout";
08
collection.each(function(){
09
var i = {};
10
i.target = this;
11
$(i.target).on(tstart,function(e){
12
var p = "touches" in e ? e.touches[0] : (isTouch ? window.event.touches[0] : window.event);
13
i.startX = p.clientX;
14
i.startY = p.clientY;
15
i.endX = p.clientX;
16
i.endY = p.clientY;
17
i.startTime = + new Date;
18
});
19
$(i.target).on(tmove,function(e){
20
var p = "touches" in e ? e.touches[0] : (isTouch ? window.event.touches[0] : window.event);
21
i.endX = p.clientX;
22
i.endY = p.clientY;
23
});
24
$(i.target).on(tend,function(e){
25
if((+ new Date)-i.startTime<300){
26
if(Math.abs(i.endX-i.startX)+Math.abs(i.endY-i.startY)<20){
27
var e = e || window.event;
28
e.preventDefault();
29
fn.call(i.target);
30
http://www.huiyi8.com/biaoqian/taobao/}
31
}
32
i.startTime = undefined;
33
i.startX = undefined;
34
i.startY = undefined;
35
i.endX = undefined;
36
i.endY = undefined;
37
});
38
});
39
return collection;
40
}

转载于:https://www.cnblogs.com/xrkbgan/p/3768451.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值