-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathtouch.js
84 lines (74 loc) · 2.48 KB
/
touch.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
/**
* 手指滑动组件
* @author helijun
* @return {[type]} [description]
*/
(function(win) {
var touchMain = {
init: function(){
var self = touchMain;
},
options: {
startX: null,
startY: null
},
touchStart: function(event){
var self = touchMain;
try{
var touch = event.touches[0], //获取第一个触点
x = Number(touch.pageX), //页面触点X坐标
y = Number(touch.pageY); //页面触点Y坐标
//记录触点初始位置
self.options.startX = x;
self.options.startY = y;
}catch(e){
console.log(e.message)
}
},
/**
* 滑动时判断下滑、上滑
* @param {[type]} event
* @param {[type]} upcallback [上滑回调函数]
* @param {[type]} downcallback [下滑回调函数]
*/
touchMove: function(event,upcallback,downcallback){
var self = touchMain;
try{
var touch = event.touches[0], //获取第一个触点
x = Number(touch.pageX), //页面触点X坐标
y = Number(touch.pageY); //页面触点Y坐标
//判断滑动方向
if (y - self.options.startY > 0) {
//alert('下滑了!');
downcallback && downcallback();
}else{
//alert('上滑了!');
upcallback && upcallback();
}
}catch(e){
console.log('滑动时出错:',e.message)
}
},
touchEnd: function(event){
var self = touchMain;
try{
var touch = event.touches[0], //获取第一个触点
x = Number(touch.pageX), //页面触点X坐标
y = Number(touch.pageY); //页面触点Y坐标
//判断滑动方向
if (y - self.options.startY > 0) {
console.log('下滑了!');
}else{
console.log('上滑了!');
}
}catch(e){
console.log(e.message)
}
},
render: function(){
var self = touchMain;
}
}
touchMain.init();
win.touchMain = touchMain;
})(window);