1
1
/**
2
2
* @author 剧中人
3
3
* @github https://github.com/bh-lay/toucher
4
- * @modified 2014-6-3 14:14
4
+ * @modified 2014-6-3 15:48
5
5
*
6
6
*/
7
7
window . util = window . util || { } ;
@@ -89,7 +89,7 @@ window.util.toucher = window.util.toucher || function (dom){
89
89
//符合事件委托,执行
90
90
if ( hasClass ( target . className , classStr ) ) {
91
91
//返回false停止事件冒泡及后续事件,其余继续执行
92
- if ( callback . call ( target , e ) == false ) {
92
+ if ( event_callback ( eventName , callback , target , e ) == false ) {
93
93
return
94
94
}
95
95
} else {
@@ -111,15 +111,30 @@ window.util.toucher = window.util.toucher || function (dom){
111
111
var callback = rest_events [ i ] [ 'fn' ] ;
112
112
//未指定事件委托,直接执行
113
113
if ( classStr == null ) {
114
- callback . call ( target , e ) ;
114
+ event_callback ( eventName , callback , target , e ) ;
115
115
}
116
116
}
117
117
return ;
118
118
}
119
119
}
120
120
}
121
-
122
-
121
+
122
+ /**
123
+ * 执行绑定的回调函数,并创建一个事件对象
124
+ */
125
+ function event_callback ( name , fn , dom , e ) {
126
+ var touch = e . touches . length ? e . touches [ 0 ] : { } ;
127
+
128
+ var newE = {
129
+ 'type' : name ,
130
+ 'pageX' : touch . clientX || 0 ,
131
+ 'pageY' : touch . clientY || 0 ,
132
+ 'screenX' : touch . screenX || 0 ,
133
+ 'screenY' : touch . screenY || 0
134
+ } ;
135
+
136
+ return fn . call ( dom , newE ) ;
137
+ }
123
138
/**
124
139
* 判断swipe方向
125
140
*/
@@ -147,7 +162,8 @@ window.util.toucher = window.util.toucher || function (dom){
147
162
var longTap ;
148
163
//记录当前事件是否已为等待结束的状态
149
164
var isActive = false ;
150
-
165
+ //记录有坐标信息的事件
166
+ var eventMark = null ;
151
167
//单次用户操作结束
152
168
function actionOver ( e ) {
153
169
isActive = false ;
@@ -156,6 +172,9 @@ window.util.toucher = window.util.toucher || function (dom){
156
172
}
157
173
158
174
function touchStart ( e ) {
175
+ //缓存事件
176
+ eventMark = e ;
177
+
159
178
x1 = e . touches [ 0 ] . pageX ;
160
179
y1 = e . touches [ 0 ] . pageY ;
161
180
x2 = 0 ;
@@ -172,7 +191,8 @@ window.util.toucher = window.util.toucher || function (dom){
172
191
} , 500 ) ;
173
192
}
174
193
function touchend ( e ) {
175
- EMIT . call ( this_touch , 'swipeEnd' , e ) ;
194
+ //touchend中,拿不到坐标位置信息,故使用全局保存下的事件
195
+ EMIT . call ( this_touch , 'swipeEnd' , eventMark ) ;
176
196
if ( ! isActive ) {
177
197
return
178
198
}
@@ -181,18 +201,21 @@ window.util.toucher = window.util.toucher || function (dom){
181
201
touchDelay = setTimeout ( function ( ) {
182
202
//断定此次事件为轻击事件
183
203
actionOver ( ) ;
184
- EMIT . call ( this_touch , 'singleTap' , e ) ;
204
+ EMIT . call ( this_touch , 'singleTap' , eventMark ) ;
185
205
} , 250 ) ;
186
206
} else {
187
207
clearTimeout ( touchDelay ) ;
188
208
actionOver ( e ) ;
189
209
//断定此次事件为连续两次轻击事件
190
- EMIT . call ( this_touch , 'doubleTap' , e ) ;
210
+ EMIT . call ( this_touch , 'doubleTap' , eventMark ) ;
191
211
}
192
212
lastTouchTime = now ;
193
213
}
194
214
195
215
function touchmove ( e ) {
216
+ //缓存事件
217
+ eventMark = e ;
218
+
196
219
//断定此次事件为移动事件
197
220
EMIT . call ( this_touch , 'swipe' , e ) ;
198
221
@@ -211,6 +234,7 @@ window.util.toucher = window.util.toucher || function (dom){
211
234
EMIT . call ( this_touch , 'singleTap' , e ) ;
212
235
}
213
236
actionOver ( e ) ;
237
+ //是否阻止浏览器默认事件
214
238
if ( this_touch . preventDefault ) {
215
239
e . preventDefault ( ) ;
216
240
e . stopPropagation ( ) ;
0 commit comments