Skip to content

Commit 6f33e58

Browse files
committed
为移动事件增加位置信息
1 parent e33722b commit 6f33e58

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

asset/toucher.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ window.util.toucher = window.util.toucher || function (dom){
4343
className = null;
4444
fn = a;
4545
}
46-
//事件名存在且callback合法,进行监听
46+
//事件名存在且callback合法,进行监听绑定
4747
if(eventName.length > 0 && typeof(fn) == 'function'){
4848
//事件堆无该事件,创建一个事件堆
4949
if(!this._events[eventName]){
@@ -68,14 +68,15 @@ window.util.toucher = window.util.toucher || function (dom){
6868
*/
6969
function EMIT(eventName,e){
7070
this._events = this._events || {};
71-
//事件堆无该事件,结束运行
71+
//事件堆无该事件,结束触发
7272
if(!this._events[eventName]){
7373
return
7474
}
75-
//尚未被执行掉的事件绑定
75+
//记录尚未被执行掉的事件绑定
7676
var rest_events = this._events[eventName];
77+
78+
//从事件源:target开始向上冒泡
7779
var target = e.target;
78-
//从target开始向上冒泡
7980
while (1) {
8081
//当前需要校验的事件集
8182
var eventsList = rest_events;
@@ -93,7 +94,7 @@ window.util.toucher = window.util.toucher || function (dom){
9394
return
9495
}
9596
}else{
96-
//不符合执行条件,压回到尚未执行掉的列表中
97+
//不符合执行条件,压回到尚未执行掉的列表中
9798
rest_events.push(eventsList[i]);
9899
}
99100
}
@@ -121,18 +122,26 @@ window.util.toucher = window.util.toucher || function (dom){
121122

122123
/**
123124
* 执行绑定的回调函数,并创建一个事件对象
125+
* @param[string]事件名
126+
* @param[function]被执行掉的函数
127+
* @param[object]指向的dom
128+
* @param[object]原生event对象
124129
*/
125130
function event_callback(name,fn,dom,e){
126131
var touch = e.touches.length ? e.touches[0] : {};
127132

128133
var newE = {
129134
'type' : name,
130135
'pageX' : touch.clientX || 0,
131-
'pageY' : touch.clientY || 0,
132-
'screenX' : touch.screenX || 0,
133-
'screenY' : touch.screenY || 0
136+
'pageY' : touch.clientY || 0
134137
};
135-
138+
//为swipe事件增加交互初始位置及移动距离
139+
if(name == 'swipe' && e.startPosition){
140+
newE.startX = e.startPosition['pageX'],
141+
newE.startY = e.startPosition['pageY'],
142+
newE.moveX = newE.pageX - newE.startX,
143+
newE.moveY = newE.pageY - newE.startY
144+
}
136145
return fn.call(dom,newE);
137146
}
138147
/**
@@ -170,7 +179,8 @@ window.util.toucher = window.util.toucher || function (dom){
170179
clearTimeout(longTap);
171180
clearTimeout(touchDelay);
172181
}
173-
182+
183+
//触屏开始
174184
function touchStart(e){
175185
//缓存事件
176186
eventMark = e;
@@ -190,6 +200,7 @@ window.util.toucher = window.util.toucher || function (dom){
190200
EMIT.call(this_touch,'longTap',e);
191201
},500);
192202
}
203+
//触屏结束
193204
function touchend(e){
194205
//touchend中,拿不到坐标位置信息,故使用全局保存下的事件
195206
EMIT.call(this_touch,'swipeEnd',eventMark);
@@ -211,11 +222,16 @@ window.util.toucher = window.util.toucher || function (dom){
211222
}
212223
lastTouchTime = now;
213224
}
214-
225+
226+
//手指移动
215227
function touchmove(e){
216228
//缓存事件
217229
eventMark = e;
218-
230+
//在原生事件基础上记录初始位置(为swipe事件增加参数传递)
231+
e.startPosition = {
232+
'pageX' : x1,
233+
'pageY' : y1
234+
};
219235
//断定此次事件为移动事件
220236
EMIT.call(this_touch,'swipe',e);
221237

@@ -269,8 +285,7 @@ window.util.toucher = window.util.toucher || function (dom){
269285
DOM.addEventListener('MSPointerCancel',actionOver);
270286
DOM.addEventListener('pointercancel',actionOver);
271287
}
272-
273-
288+
274289
/**
275290
* touch类
276291
*
@@ -285,6 +300,8 @@ window.util.toucher = window.util.toucher || function (dom){
285300
}
286301
//拓展事件绑定方法
287302
touch.prototype['on'] = ON;
303+
304+
288305
//对外提供接口
289306
exports.init = touch;
290307
})(util.toucher);

0 commit comments

Comments
 (0)