Skip to content

Commit 6c20bfc

Browse files
committed
封装伪event对象
1 parent d8f78f9 commit 6c20bfc

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

asset/toucher.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @author 剧中人
33
* @github https://github.com/bh-lay/toucher
4-
* @modified 2014-6-3 14:14
4+
* @modified 2014-6-3 15:48
55
*
66
*/
77
window.util = window.util || {};
@@ -89,7 +89,7 @@ window.util.toucher = window.util.toucher || function (dom){
8989
//符合事件委托,执行
9090
if(hasClass(target.className,classStr)){
9191
//返回false停止事件冒泡及后续事件,其余继续执行
92-
if(callback.call(target,e) == false){
92+
if(event_callback(eventName,callback,target,e) == false){
9393
return
9494
}
9595
}else{
@@ -111,15 +111,30 @@ window.util.toucher = window.util.toucher || function (dom){
111111
var callback = rest_events[i]['fn'];
112112
//未指定事件委托,直接执行
113113
if(classStr == null){
114-
callback.call(target,e);
114+
event_callback(eventName,callback,target,e);
115115
}
116116
}
117117
return;
118118
}
119119
}
120120
}
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+
}
123138
/**
124139
* 判断swipe方向
125140
*/
@@ -147,7 +162,8 @@ window.util.toucher = window.util.toucher || function (dom){
147162
var longTap;
148163
//记录当前事件是否已为等待结束的状态
149164
var isActive = false;
150-
165+
//记录有坐标信息的事件
166+
var eventMark = null;
151167
//单次用户操作结束
152168
function actionOver(e){
153169
isActive = false;
@@ -156,6 +172,9 @@ window.util.toucher = window.util.toucher || function (dom){
156172
}
157173

158174
function touchStart(e){
175+
//缓存事件
176+
eventMark = e;
177+
159178
x1 = e.touches[0].pageX;
160179
y1 = e.touches[0].pageY;
161180
x2 = 0;
@@ -172,7 +191,8 @@ window.util.toucher = window.util.toucher || function (dom){
172191
},500);
173192
}
174193
function touchend(e){
175-
EMIT.call(this_touch,'swipeEnd',e);
194+
//touchend中,拿不到坐标位置信息,故使用全局保存下的事件
195+
EMIT.call(this_touch,'swipeEnd',eventMark);
176196
if(!isActive){
177197
return
178198
}
@@ -181,18 +201,21 @@ window.util.toucher = window.util.toucher || function (dom){
181201
touchDelay = setTimeout(function(){
182202
//断定此次事件为轻击事件
183203
actionOver();
184-
EMIT.call(this_touch,'singleTap',e);
204+
EMIT.call(this_touch,'singleTap',eventMark);
185205
},250);
186206
}else{
187207
clearTimeout(touchDelay);
188208
actionOver(e);
189209
//断定此次事件为连续两次轻击事件
190-
EMIT.call(this_touch,'doubleTap',e);
210+
EMIT.call(this_touch,'doubleTap',eventMark);
191211
}
192212
lastTouchTime = now;
193213
}
194214

195215
function touchmove(e){
216+
//缓存事件
217+
eventMark = e;
218+
196219
//断定此次事件为移动事件
197220
EMIT.call(this_touch,'swipe',e);
198221

@@ -211,6 +234,7 @@ window.util.toucher = window.util.toucher || function (dom){
211234
EMIT.call(this_touch,'singleTap',e);
212235
}
213236
actionOver(e);
237+
//是否阻止浏览器默认事件
214238
if(this_touch.preventDefault){
215239
e.preventDefault();
216240
e.stopPropagation();

0 commit comments

Comments
 (0)