-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathJTouch.js
483 lines (466 loc) · 14.9 KB
/
JTouch.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
JTouch v1.1 2013-06-04
https://github.com/liutian1937/JTouch
ok8008@yeah.net
*/
(function () {
var TapTimes = 0,
TapTimeout,
LongTimeout,
LastDirect = '';
var TouchCommon = {
isTouch : function () {
return document.hasOwnProperty('ontouchstart');
},
isMSPointer : function () {
return window.navigator.msPointerEnabled;
},
isPointer : function () {
return window.navigator.pointerEnabled;
},
isIE : function () {
return (document.all) ? true : false;
},
isLeft : function (e){
if (document.all && e.button === 1 || e.button === 0){
return true;
};
},
getTime : function () {
return Date.now() || new Date().getTime() ;
},
stopDefault : function (e) {
e = e || window.event;
if(e && e.preventDefault){
e.preventDefault();
}else{
e.returnValue=false;
}
}
};
var TouchAction = function (element,event,touch) {
/*
函数TouchAction主要针对点击,滑动的处理,手势变换用下面Gesture
点击事件:Tap,DoubleTap,LongTap,Swipe(滑动),Flick(轻拂)
*/
this.evt = event;
this.touch = touch || undefined;
this.startX = this.currentX = touch?touch.screenX:event.pageX; //初始化点击开始的位置,X
this.startY = this.currentY = touch?touch.screenY:event.pageY; //初始化点击开始的位置,Y
this.eventType = null; //初始化事件类型
this.startTime = TouchCommon.getTime(); //点击开始计时,初始点击时间
this.checkLongTap(element); //检查是否是长按
this.data = {};
this.swipeData = {
x : this.currentX,
y : this.currentY
};
LastDirect = '';
};
TouchAction.prototype = {
getTapType : function () {
//判断点击类型,tap与doubletap
var _this = this;
TapTimes = (TapTimes == 0) ? 1 : TapTimes + 1;
if (TapTimes == 1) {
_this.eventType = 'tap';
} else if (TapTimes == 2) {
_this.eventType = 'doubletap';
clearTimeout(TapTimeout);
} else {
TapTimes = 0;
};
},
checkLongTap : function (element) {
//长按检测,longtap
var _this = this;
clearTimeout(LongTimeout);
LongTimeout = setTimeout(function () {
_this.eventType = 'longtap';
_this.touchCallback(element);
}, 500);
},
move : function (element,touch) {
//手指在对象上滑动
var _this = this, offsetX, offsetY, timeStamp;
clearTimeout(LongTimeout); //取消长按检测
_this.currentX = _this.touch?touch.screenX:touch.pageX; //获取当前坐标值,pageX为到窗口的距离
_this.currentY = _this.touch?touch.screenY:touch.pageY;
offsetX = _this.currentX - _this.startX; //计算手指滑动的横向长度
offsetY = _this.currentY - _this.startY; //计算手指滑动的纵向长度
/*
move的时候定义事件类型eventType:swipe 或者 hold
*/
if (_this.eventType == 'longtap' || _this.eventType == 'hold') {
_this.eventType = 'hold';
} else {
_this.eventType = 'swipe';
};
if(LastDirect != ''){
if(LastDirect == 'Horizontal'){
_this.data['direction'] = offsetX > 0 ? 'right' : 'left';
}else{
_this.data['direction'] = offsetY > 0 ? 'down' : 'up';
}
}else{
if (Math.abs(offsetX) > Math.abs(offsetY)) {
/*
如果上次的移动方向是左右
或者横向滑动大于纵向,是左右滑动
*/
_this.data['direction'] = offsetX > 0 ? 'right' : 'left';
LastDirect = 'Horizontal';
}else {
/*
纵向滑动大于横向,是上下滑动
*/
_this.data['direction'] = offsetY > 0 ? 'down' : 'up';
LastDirect = 'Vertical';
};
};
_this.data['x'] = offsetX;
_this.data['y'] = offsetY;
timeStamp = TouchCommon.getTime();
if(timeStamp - _this.startTime > 300){
_this.startTime = timeStamp;
_this.swipeData = {
x : _this.currentX,
y : _this.currentY
}
}
_this.touchCallback(element); //执行回调函数
},
process : function (element) {
//touch结束后执行
var _this = this, offsetX, offsetY;
offsetX = _this.currentX - _this.startX; //移动横向距离
offsetY = _this.currentY - _this.startY; //移动纵向距离
if (_this.eventType && _this.eventType !== 'swipe' && _this.eventType !== 'hold') {
return false;
};
clearTimeout(LongTimeout); //清除长按检测
if (offsetX == 0 && offsetY == 0) {
//两次触摸没有距离移动
_this.getTapType();
if (TapTimes == 1) {
TapTimeout = setTimeout(function () {
_this.touchCallback(element);
}, 250);
} else if (TapTimes == 2) {
_this.touchCallback(element);
}
} else if (Math.abs(offsetY) > 0 || Math.abs(offsetX) > 0) {
_this.data['x'] = offsetX;
_this.data['y'] = offsetY;
_this.data['time'] = TouchCommon.getTime() - _this.startTime;
if (_this.data['time'] < 200) {
_this.data['speed'] = Math.max(Math.abs(_this.currentX - _this.swipeData['x'])/_this.data['time'],Math.abs(_this.currentY - _this.swipeData['y'])/_this.data['time']);
if(_this.data['speed'] > 0.5){
//时间小于300,动作为轻拂:flick
if (Math.abs(offsetY) > Math.abs(offsetX)) {
_this.data['direction'] = offsetY > 0 ? 'down' : 'up';
} else {
_this.data['direction'] = offsetX > 0 ? 'right' : 'left';
}
_this.eventType = 'flick';
}else{
_this.data['status'] = 'end';
}
}else {
//滑动结束,swipe end
_this.data['status'] = 'end';
};
_this.touchCallback(element);
};
},
touchCallback : function (element) {
//回调函数
var _this = this, len = (this.touch) ? this.evt.changedTouches.length : 1;
_this.data['fingerNum'] = len;
if (!_this.touch || _this.touch.identifier == _this.evt.changedTouches[len - 1].identifier) {
if (element.typeFn[_this.eventType])
element.typeFn[_this.eventType](_this.evt, _this.data); //执行函数
TapTimes = 0;
}
}
};
var Gesture = function (event, element) {
this.data = {};
this.eventType = null;
this.rotateActive = this.pinchActive = false;
};
Gesture.prototype = {
change : function (event, element) {
var _this = this, diffAngle, diffDistance;
if (element.objEvent.touches.length == 2) {
if (!_this.startData) {
_this.startData = _this.getData(element);
};
_this.currentData = _this.getData(element); //获取两根手指的坐标
diffAngle = _this.getAngle(_this.startData) - _this.getAngle(_this.currentData);
diffDistance = _this.getDistance(_this.startData) - _this.getDistance(_this.currentData);
if (Math.abs(diffAngle) > 10 || _this.rotateActive) {
_this.eventType = 'rotate';
_this.data['direction'] = (event.rotation < 0) ? 'left' : 'right';
_this.data['rotation'] = event.rotation;
_this.gestureCallback(event, element, _this.data);
_this.rotateActive = true;
};
if (Math.abs(diffDistance) > 10 || _this.pinchActive) {
_this.eventType = 'pinch';
_this.data['type'] = (event.scale < 1) ? 'in' : 'out';
_this.data['scale'] = event.scale;
_this.gestureCallback(event, element, _this.data);
_this.pinchActive = true;
};
};
},
end : function (event, element) {
var _this = this;
_this.data['rotation'] = event.rotation;
_this.data['scale'] = event.scale;
_this.data['status'] = 'end';
_this.gestureCallback(event, element, _this.data);
},
gestureCallback : function (event, element, data) {
var _this = this;
if (element.typeFn[_this.eventType])
element.typeFn[_this.eventType](event, data);
},
getData : function (element) {
var _this = this, touchList = element.objEvent.touches, ret = [];
for (var i = 0; i < touchList.length; i++) {
ret.push({
x : touchList[i].screenX ,
y : touchList[i].screenY
});
};
return ret;
},
getAngle : function (data) {
var A = data[0], B = data[1], angle = Math.atan((B.y - A.y) * -1 / (B.x - A.x)) * (180 / Math.PI);
if (angle < 0) {
return angle + 180;
} else {
return angle;
}
},
getDistance : function (data) {
var A = data[0], B = data[1];
return Math.sqrt((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y)) * -1;
}
};
var Touch = function (obj) {
var _this = this;
if(!(this instanceof Touch)){
return new Touch(obj);
};
_this.obj = obj || window;
_this.init();
};
Touch.prototype = {
on : function (type, callback) {
this.typeFn[type] = callback; //给typeFn添加对应的事件
return this;
},
stopEvent : function (){
//是否阻止默认事件
this.stopDefault = true;
return this;
},
init : function () {
var _this = this, mousewheel;
_this.touches = {}; //touch对象哈希表
_this.typeFn = {}; //点击类型哈希表
_this.gesture = null;
_this.handleHash = {};
if(TouchCommon.isTouch()){
//是否支持touch事件
_this.bind(_this.obj,'touchstart',_this.touchStart);
_this.bind(_this.obj,'touchmove',_this.touchMove);
_this.bind(_this.obj,'touchend',_this.touchEnd);
_this.bind(_this.obj,'touchcancel',_this.touchCancel);
_this.bind(_this.obj,'gesturestart',_this.gestureStart);
_this.bind(_this.obj,'gesturechange',_this.gestureChange);
_this.bind(_this.obj,'gestureend',_this.gestureEnd);
} else if(TouchCommon.isPointer() || TouchCommon.isMSPointer()) {
_this.bind(_this.obj,'MSPointerDown',_this.mouseStart);
_this.bind(_this.obj,'MSPointerUp',_this.mouseEnd);
_this.bind(_this.obj,'MSPointerMove',_this.mouseMove);
_this.bind(_this.obj,'mousewheel',_this.mouseWheel);
} else {
mousewheel = (document.hasOwnProperty('onmousewheel')) ? "mousewheel" : "DOMMouseScroll" ;
_this.bind(_this.obj,'mousedown',_this.mouseStart);
_this.bind(_this.obj,'click',_this.clickFn);
_this.bind(window,'mouseup',_this.mouseEnd);
_this.bind(_this.obj,mousewheel,_this.mouseWheel);
};
},
touchStart : function (event) {
var _this = this;
if(_this.stopDefault){
TouchCommon.stopDefault(event);
};
_this.touches = {};
_this.objEvent = event;
_this.touchLoop(event, function (touch) {
_this.touches[touch.identifier] = new TouchAction(_this,event,touch);
});
if (_this.typeFn['start']) {
_this.typeFn['start'](event);
};
},
touchMove : function (event) {
var _this = this;
event.preventDefault(); //阻止浏览器默认动作
_this.objEvent = event;
_this.touchLoop(event, function (touch) {
var touchTarget = _this.touches[touch.identifier];
if (touchTarget) {
touchTarget.move(_this,touch); //touchMove时执行函数
};
});
},
touchEnd : function (event) {
var _this = this;
if(_this.stopDefault){
TouchCommon.stopDefault(event);
};
_this.touchLoop(event, function (touch) {
_this.touchClear(touch, false);
});
if (_this.typeFn['end']) {
_this.typeFn['end'](event);
};
},
touchCancel : function (event) {
var _this = this;
event.preventDefault(); //阻止浏览器默认动作
_this.touchLoop(event, function (touch) {
_this.touchClear(touch, true);
});
},
touchLoop : function (event, callback) {
var len = event.changedTouches.length;
callback(event.changedTouches[len - 1]);
},
touchClear : function (touch, cancelled) {
var _this = this, touchTarget;
if (!cancelled) {
touchTarget = _this.touches[touch.identifier];
if (touchTarget) {
touchTarget.process(_this); //touchEnd时执行函数
};
};
delete _this.touches[touch.identifier];
},
mouseStart : function (event) {
var _this = this;
if(!TouchCommon.isLeft(event)){
//如果不是左键,阻止进程
return false;
};
event.preventDefault(); //阻止浏览器默认动作
_this.touches = {};
_this.objEvent = event;
_this.touches = new TouchAction(_this,event); //实例化TouchAction
_this.bind(window,'mousemove',_this.mouseMove);//给window绑定mousemove事件
if (_this.typeFn['start']) {
_this.typeFn['start'](event);
};
},
mouseMove : function (event) {
var _this = this;
event.preventDefault(); //阻止浏览器默认动作
_this.objEvent = event;
if (_this.touches instanceof TouchAction){
_this.touches.move(_this,event);
};
},
mouseEnd : function (event) {
var _this = this;
event.preventDefault(); //阻止浏览器默认动作
(TouchCommon.isIE())? event.cancelBubble = true : event.stopPropagation();//阻止冒泡
if(!(_this.touches instanceof TouchAction)){
return false;
};
_this.touches.process(_this);
_this.touches = {};
_this.unbind(window,'mousemove');//取消window的事件绑定
if (_this.typeFn['end']) {
_this.typeFn['end'](event);
};
},
mouseWheel : function (event) {
var _this = this, data = {}, delta = event.wheelDelta ? (event.wheelDelta / 120) : (- event.detail / 3);
data['direction'] = (delta > 0)?'up':'down';
if (_this.typeFn['mousewheel']) {
_this.typeFn['mousewheel'](event,data);
};
},
clickFn : function (event) {
var _this = this, target = event.target || event.srcElement;
if(_this.stopDefault){
TouchCommon.stopDefault(event);
};
},
gestureStart : function (event) {
var _this = this;
event.preventDefault(); //阻止浏览器默认动作
_this.gesture = new Gesture(event, _this);
},
gestureChange : function (event) {
var _this = this;
event.preventDefault();
_this.gesture.change(event, _this);
},
gestureEnd : function (event) {
var _this = this;
_this.gesture.end(event, _this);
_this.gesture = null;
},
bind : (function() {
if (window.addEventListener) {
return function(el, type, fn, capture) {
var _this = this;
el.addEventListener(type, function(){
fn.call(_this,arguments[0]);
_this.handleHash[type] = arguments.callee;
}, (capture));
};
} else if (window.attachEvent) {
return function(el, type, fn, capture) {
var _this = this;
el.attachEvent("on" + type, function(){
fn.call(_this,arguments[0]);
_this.handleHash[type] = arguments.callee;
});
};
}
})(),
unbind : (function(){
if (window.addEventListener) {
return function(el, type ) {
var _this = this;
if(_this.handleHash[type]){
el.removeEventListener(type, _this.handleHash[type]);
};
};
} else if (window.attachEvent) {
return function(el, type) {
var _this = this;
if(_this.handleHash[type]){
el.detachEvent("on" + type, _this.handleHash[type]);
};
};
}
})(),
destory : function () {
var _this = this, i;
for(i in _this.handleHash){
_this.unbind(_this.obj,i,_this.handleHash[i]);
}
}
};
window.JTouch = Touch;
}());