Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

touchable detect;

  • Loading branch information...
commit 91eb6e644b2c5dfd832a2bb80abf0d8090a6a6e8 1 parent ac67e5b
@switer authored
Showing with 36 additions and 12 deletions.
  1. +36 −12 fastap.js
View
48 fastap.js
@@ -1,12 +1,30 @@
-!function () {
- var events = [];
+! function() {
+ var events = [],
+ touchable = isTouchDevice;
+
+ // touch event support detect
+ function isTouchDevice() {
+ try {
+ document.createEvent("TouchEvent");
+ return true;
+ } catch (e) {
+ return false;
+ }
+ }
+
/**
* 覆盖zepto默认的tap
**/
$.fn.tap = function(handler) {
var $con = this;
- function tapHandler (event) {
+ if (!touchable) {
+ $con.on('click', handler);
+ return;
+ }
+
+ // support touch event
+ function tapHandler(event) {
var $tar = $(event.target);
if (!$tar) return;
@@ -53,21 +71,27 @@
/**
* 取消监听tap事件(必需为覆盖zepto的tap事件)
**/
- $.fn.tapOff = function (handler) {
+ $.fn.tapOff = function(handler) {
+
+ if (!touchable) {
+ this.of('click', handler);
+ return;
+ }
- for (var i = 0; i < events.length; i ++) {
+ // touch events off
+ for (var i = 0; i < events.length; i++) {
var evtObj = events[i];
- isMatched = false;
+ isMatched = false;
if (evtObj) {
- this.each(function (index, el) {
- evtObj.$el.each(function (index, target) {
- if (target == el) {
+ this.each(function(index, el) {
+ evtObj.$el.each(function(index, target) {
+ if (target == el) {
isMatched = true;
return true;
- }
- });
- if (isMatched) return true;
+ }
+ });
+ if (isMatched) return true;
});
if (handler == evtObj.handler && isMatched) {
this.off('touchstart', evtObj.listener);
Please sign in to comment.
Something went wrong with that request. Please try again.