Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

wrapper with bang;

  • Loading branch information...
commit ac67e5b89cd68d0ee9400b661d5c3d759e1bf233 1 parent 8aa4805
@switer authored
Showing with 65 additions and 64 deletions.
  1. +65 −64 fastap.js
View
129 fastap.js
@@ -1,78 +1,79 @@
-var events = [];
+!function () {
+ var events = [];
+ /**
+ * 覆盖zepto默认的tap
+ **/
+ $.fn.tap = function(handler) {
+ var $con = this;
-/**
- * 覆盖zepto默认的tap
- **/
-$.fn.tap = function(handler) {
- var $con = this;
+ function tapHandler (event) {
+ var $tar = $(event.target);
+ if (!$tar) return;
- function tapHandler (event) {
- var $tar = $(event.target);
- if (!$tar) return;
-
- $con.off('touchmove', scrollHandler);
- $tar.off('touchend', endHandler);
-
- var isSroll = false,
- isEnd = false,
- isFeed = false,
- delay = 50;
-
- function scrollHandler(e) {
- isSroll = true;
$con.off('touchmove', scrollHandler);
- return;
- }
-
- function endHandler(e) {
- isEnd = true;
$tar.off('touchend', endHandler);
- if (isSroll) {
+ var isSroll = false,
+ isEnd = false,
+ isFeed = false,
+ delay = 50;
+
+ function scrollHandler(e) {
+ isSroll = true;
+ $con.off('touchmove', scrollHandler);
return;
}
+
+ function endHandler(e) {
+ isEnd = true;
+ $tar.off('touchend', endHandler);
+
+ if (isSroll) {
+ return;
+ }
+ setTimeout(function() {
+ isFeed && handler && handler(event);
+ }, delay + 10);
+ }
+ $con.on('touchmove', scrollHandler);
+ $tar.on('touchend', endHandler);
+
setTimeout(function() {
- isFeed && handler && handler(event);
- }, delay + 10);
+ isFeed = true;
+ }, delay);
}
- $con.on('touchmove', scrollHandler);
- $tar.on('touchend', endHandler);
-
- setTimeout(function() {
- isFeed = true;
- }, delay);
- }
- events.push({
- $el: this,
- listener: tapHandler,
- handler: handler
- });
- $con.on('touchstart', tapHandler);
-};
+ events.push({
+ $el: this,
+ listener: tapHandler,
+ handler: handler
+ });
+ $con.on('touchstart', tapHandler);
+ };
-/**
- * 取消监听tap事件(必需为覆盖zepto的tap事件)
- **/
-$.fn.tapOff = function (handler) {
+ /**
+ * 取消监听tap事件(必需为覆盖zepto的tap事件)
+ **/
+ $.fn.tapOff = function (handler) {
- for (var i = 0; i < events.length; i ++) {
- var evtObj = events[i];
- isMatched = false;
+ 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;
+ 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;
+ }
}
}
- }
-};
+ };
+}();
Please sign in to comment.
Something went wrong with that request. Please try again.