dcae17f96eff4d7a9b06804e8991999c7b00c2fe.svn-base
3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(function ($) {
/**
* If no touch events are available map touch events to corresponding mouse events.
**/
if(!('ontouchstart' in window)){
var _fakeCallbacks = {}; // store the faked callbacks so that they can be unbound
function touch2mouse(context,args){
var type = args[0],//事件类型
t = type ,matched = false;
if ((typeof type) == "object") {
return args;
}
if(type.indexOf('touchstart')>-1){
matched = true;
t = t.replace('touchstart','mousedown');
}else if(t.indexOf('touchend')>-1){
matched = true;
t = t.replace('touchend','mouseup');
}else if(t.indexOf('touchmove')>-1){
matched = true;
t = t.replace('touchmove','mousemove');
}
if(matched){//匹配到touch事件
var ci = -1;
//找出callback,需要更换里面的event对象的属性
for(var i =1;i<args.length;i++){
if(typeof args[i] == 'function'){
ci = i;
break;
}
}
args[0] = t;
if(ci > -1){
args[ci] = fakeTouches(type, args[ci] , context);
}
}
return args;
}
function fakeTouches(type, callback, context) {
// wrap the callback with a function that adds a fake
// touches property to the event.
return _fakeCallbacks[callback] = function (event) {
if(event.liveFired)context = this;//if it is delegate event,change context to target element
if (event.button) {
return false;
}
event.touches = [{
length: 1,// 1 mouse (finger)
clientX: event.clientX,
clientY: event.clientY,
pageX: event.pageX,
pageY: event.pageY,
screenX: event.screenX,
screenY: event.screenY,
target: event.target
}];
event.touchType = type;
return callback.apply(context, [event]);
}
}
var _trigger = $.fn.trigger;
$.fn.trigger = function(event, data){
var args = Array.prototype.slice.call(arguments,0);
return _trigger.apply(this,touch2mouse(this,args));
};
var _triggerHandler = $.fn.triggerHandler;
$.fn.triggerHandler = function(event, data){
var args = Array.prototype.slice.call(arguments,0);
return _triggerHandler.apply(this, touch2mouse(this,args));
};
var _on = $.fn.on;
$.fn.on = function(event, selector, data, callback, one){
var args = Array.prototype.slice.call(arguments,0);
return _on.apply(this,touch2mouse(this,args));
}
var _off = $.fn.off;
$.fn.off = function(event, selector, callback){
var args = [event];
if(typeof selector == 'string'){
args.push(selector);
}
var result = touch2mouse(this,args).concat([_fakeCallbacks[callback]||callback]);
delete(_fakeCallbacks[callback]);
return _off.apply(this,result);
}
}
})(Zepto);