diff --git a/asset/toucher.js b/asset/toucher.js index 7b674c8..3e0209e 100644 --- a/asset/toucher.js +++ b/asset/toucher.js @@ -16,7 +16,6 @@ //提供CommonJS规范的接口 global.define && define(function(require,exports,module){ - //对外接口 return factory; }); })(this,document,function(window,document){ @@ -26,47 +25,7 @@ function hasClass(dom,classSingle){ return dom.className.match(new RegExp('(\\s|^)' + classSingle +'(\\s|$)')); } - - /** - * @method 向句柄所在对象增加事件监听 - * @description 支持链式调用 - * - * @param string 事件名 - * @param [string] 事件委托至某个class(可选) - * @param function 符合条件的事件被触发时需要执行的回调函数 - * - */ - function ON(eventStr,a,b){ - this._events = this._events || {}; - var className,fn; - if(typeof(a) == 'string'){ - className = a.replace(/^\./,''); - fn = b; - }else{ - className = null; - fn = a; - } - //检测callback是否合法,事件名参数是否存在· - if(typeof(fn) == 'function' && eventStr && eventStr.length){ - var eventNames = eventStr.split(/\s+/); - for(var i=0,total=eventNames.length;i= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down') + return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down'); } /** @@ -201,12 +162,11 @@ clearTimeout(touchDelay); } - - //断定此次事件为轻击事件 - function isSingleTap(){ - actionOver(); - EMIT.call(this_touch,'singleTap',eventMark); - } + //断定此次事件为轻击事件 + function isSingleTap(){ + actionOver(); + EMIT.call(this_touch,'singleTap',eventMark); + } //触屏开始 function touchStart(e){ //缓存事件 @@ -228,18 +188,21 @@ } //触屏结束 function touchend(e){ - //touchend中,拿不到坐标位置信息,故使用全局保存下的事件 - EMIT.call(this_touch,'swipeEnd',eventMark); + //touchend中,拿不到坐标位置信息,故使用全局保存下数据 + e.plugStartPosition = eventMark.plugStartPosition; + e.plugTouches = eventMark.touches; + + EMIT.call(this_touch,'swipeEnd',e); if(!isActive){ - return + return; } var now = new Date(); - //若未监听doubleTap,直接响应 - if(!this_touch._events.doubleTap || this_touch._events.doubleTap.length == 0){ - isSingleTap(); - }else if(now - lastTouchTime > 200){ - //延迟响应 - touchDelay = setTimeout(isSingleTap,190); + //若未监听doubleTap,直接响应 + if(!this_touch._events.doubleTap || this_touch._events.doubleTap.length == 0){ + isSingleTap(); + }else if(now - lastTouchTime > 200){ + //延迟响应 + touchDelay = setTimeout(isSingleTap,190); }else{ clearTimeout(touchDelay); actionOver(e); @@ -254,7 +217,7 @@ //缓存事件 eventMark = e; //在原生事件基础上记录初始位置(为swipe事件增加参数传递) - e.startPosition = { + e.plugStartPosition = { pageX : x1, pageY : y1 }; @@ -262,10 +225,10 @@ EMIT.call(this_touch,'swipe',e); if(!isActive){ - return + return; } - x2 = e.touches[0].pageX - y2 = e.touches[0].pageY + x2 = e.touches[0].pageX; + y2 = e.touches[0].pageY; if(Math.abs(x1-x2)>2 || Math.abs(y1-y2)>2){ //断定此次事件为移动手势 var direction = swipeDirection(x1, x2, y1, y2); @@ -310,18 +273,56 @@ * touch类 * */ - function touch(DOM,param){ + function Touch(DOM,param){ var param = param || {}; this.dom = DOM; + //存储监听事件的回调 + this._events = {}; //监听DOM原生事件 eventListener.call(this,this.dom); } - //拓展事件绑定方法 - touch.prototype['on'] = ON; + /** + * @method 增加事件监听 + * @description 支持链式调用 + * + * @param string 事件名 + * @param [string] 事件委托至某个class(可选) + * @param function 符合条件的事件被触发时需要执行的回调函数 + * + **/ + Touch.prototype.on = function ON(eventStr,a,b){ + var className,fn; + if(typeof(a) == 'string'){ + className = a.replace(/^\./,''); + fn = b; + }else{ + className = null; + fn = a; + } + //检测callback是否合法,事件名参数是否存在· + if(typeof(fn) == 'function' && eventStr && eventStr.length){ + var eventNames = eventStr.split(/\s+/); + for(var i=0,total=eventNames.length;i'); }).on('swipeStart',function(e){ $('.console3').append('swipeStart'+I()+'
'); - console.log(this,e); + //console.log(this,e); }).on('swipe',function(e){ $('.console3').append('swipe'+I()+'
'); - console.log(this,e); + //console.log(this,e); }).on('swipeEnd',function(e){ $('.console3').append('swipeEnd'+I()+'
'); - console.log(this,e); + //console.log(this,e); }).on('swipeUp',function(e){ $('.console3').append('swipeUp'+I()+'
'); }).on('swipeRight',function(e){ @@ -136,7 +136,7 @@ return false; }).on('swipeEnd','.checkA',function(e){ $('.console1').append('swipeEnd
'); - console.log(this,e); +// console.log(this,e); $(this).animate({ 'position':'static', 'top':0,