Skip to content

Commit f5b991f

Browse files
committed
移除冗余的单独配置浏览器默认事件部分,支持return false阻止方式
1 parent 1100241 commit f5b991f

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ toucher是一个面向移动端web开发,通过监听原生事件模拟手势
1717
* swipeleft:左划
1818

1919
#说明
20-
监听的对象不阻止浏览器默认事件,若要用于拖动操作,或滑动更为细腻,可增加preventDefault属性,但此时页面是不能通过拖动当前dom进行滚动的!
20+
事件触发时不阻止浏览器默认事件,若要用于拖动操作,或滑动更为细腻,可在swipe事件中使用“return false;”阻止浏览器默认事件,但此时页面是不能通过拖动当前dom进行滚动的!
2121
#DEMO
2222
请使用移动设备或使用调试工具模拟移动设备查看 [demo](http://htmlpreview.github.io/?https://github.com/bh-lay/toucher/blob/master/touch.html)
2323

@@ -26,8 +26,7 @@ toucher是一个面向移动端web开发,通过监听原生事件模拟手势
2626

2727
```javascript
2828
var myTouch = util.toucher(document.getElementById('touchBox'));
29-
//阻止浏览器默认事件
30-
//myTouch.preventDefault = true;
29+
3130
myTouch.on('singleTap',function(e){
3231
//
3332
}).on('longTap',function(e){

asset/toucher.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/**
22
* @author 剧中人
33
* @github https://github.com/bh-lay/toucher
4-
* @modified 2014-6-8 9:6
4+
* @modified 2014-6-9 16:58
55
*
66
*/
77

88

99
(function(global,doc,factoryFn){
1010
var factory = factoryFn();
11-
11+
//提供window.util.toucher()接口
1212
global.util = global.util || {};
1313
global.util.toucher = global.util.toucher || factory;
1414
//提供CommonJS规范的接口
@@ -153,7 +153,14 @@
153153
newE.moveX = newE.pageX - newE.startX,
154154
newE.moveY = newE.pageY - newE.startY
155155
}
156-
return fn.call(dom,newE);
156+
var call_result = fn.call(dom,newE);
157+
//若绑定方法返回false,阻止浏览器默认事件
158+
if(call_result == false){
159+
e.preventDefault();
160+
e.stopPropagation();
161+
}
162+
163+
return call_result;
157164
}
158165
/**
159166
* 判断swipe方向
@@ -261,11 +268,6 @@
261268
EMIT.call(this_touch,'singleTap',e);
262269
}
263270
actionOver(e);
264-
//是否阻止浏览器默认事件
265-
if(this_touch.preventDefault){
266-
e.preventDefault();
267-
e.stopPropagation();
268-
}
269271
}
270272

271273
/**
@@ -305,7 +307,6 @@
305307
var param = param || {};
306308

307309
this.dom = DOM;
308-
this.preventDefault = (typeof(param.preventDefaul)=='boolean' ? param.preventDefault : false);
309310
//监听DOM原生事件
310311
eventListener.call(this,this.dom);
311312
}

0 commit comments

Comments
 (0)