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