Skip to content

Commit

Permalink
添加tap Off 方法;
Browse files Browse the repository at this point in the history
  • Loading branch information
switer committed Mar 4, 2014
1 parent 4b69eda commit 8aa4805
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,4 +1,4 @@
fastap fastap
====== ======


Simple, fast & small Fastclick module, use touch events simulate click on touch device. Simple, fast & small Fastclick Zepto/JQuery plugin, use touch events simulate click on touch device.
43 changes: 39 additions & 4 deletions fastap.js
@@ -1,10 +1,12 @@
var events = [];

/** /**
* 覆盖zepto默认的tap * 覆盖zepto默认的tap
**/ **/
$.fn.tap = function(handler) { $.fn.tap = function(handler) {
var $con = $(this); var $con = this;


$con.on('touchstart', function(event) { function tapHandler (event) {
var $tar = $(event.target); var $tar = $(event.target);
if (!$tar) return; if (!$tar) return;


Expand All @@ -14,7 +16,7 @@ $.fn.tap = function(handler) {
var isSroll = false, var isSroll = false,
isEnd = false, isEnd = false,
isFeed = false, isFeed = false,
delay = 20; delay = 50;


function scrollHandler(e) { function scrollHandler(e) {
isSroll = true; isSroll = true;
Expand All @@ -31,13 +33,46 @@ $.fn.tap = function(handler) {
} }
setTimeout(function() { setTimeout(function() {
isFeed && handler && handler(event); isFeed && handler && handler(event);
}, 30); }, delay + 10);
} }
$con.on('touchmove', scrollHandler); $con.on('touchmove', scrollHandler);
$tar.on('touchend', endHandler); $tar.on('touchend', endHandler);


setTimeout(function() { setTimeout(function() {
isFeed = true; isFeed = true;
}, delay); }, delay);
}
events.push({
$el: this,
listener: tapHandler,
handler: handler
}); });
$con.on('touchstart', tapHandler);
}; };

/**
* 取消监听tap事件(必需为覆盖zepto的tap事件)
**/
$.fn.tapOff = function (handler) {

for (var i = 0; i < events.length; i ++) {
var evtObj = events[i];
isMatched = false;

if (evtObj) {
this.each(function (index, el) {
evtObj.$el.each(function (index, target) {
if (target == el) {
isMatched = true;
return true;
}
});
if (isMatched) return true;
});
if (handler == evtObj.handler && isMatched) {
this.off('touchstart', evtObj.listener);
events[i] = null;
}
}
}
};

0 comments on commit 8aa4805

Please sign in to comment.