Created
June 14, 2011 21:46
-
-
Save jxson/1025987 to your computer and use it in GitHub Desktop.
An example implementation on $('selector').tap();
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
// DON'T EVER USE AN ALERT INSIDE THE CALLBACK. | |
$.fn.tap = function(callback) { | |
if (!this) return false; | |
return this.each(function() { | |
var element = this; | |
if (element.touchHandler) { return; } | |
element.touchHandler = { | |
element: element, | |
callback: callback, | |
origin: { x: 0, y: 0 }, | |
destination: { x: 0, y: 0 }, | |
threshold: 4, | |
withinThreshold: function(){ | |
// console.debug('destination.x: ' + destination.x + ' destination.y: ' + destination.y) | |
var changeX = this.origin.x - (this.destination.x), | |
changeY = this.origin.y - (this.destination.y); | |
// console.debug("changeX: " + changeX + " changeY: " + changeY) | |
withinY = changeY < this.threshold && changeY > (this.threshold*-1); | |
withinX = changeX < this.threshold && changeX > (this.threshold*-1); | |
// console.debug("withinX: " + withinX + " withinY: " + withinY) | |
return withinY || withinX; | |
}, | |
handleEvent: function (e) { | |
if (e.type == 'touchstart') { this.touchStart(e); } | |
if (e.type == 'touchmove') { this.touchMove(e); } | |
if (e.type == 'touchend') { this.touchEnd(e); } | |
if (e.type == 'touchcancel') { this.touchCancel(e); } | |
}, | |
touchStart: function(event){ | |
// console.debug('touchStart') | |
this.origin.x = event.targetTouches[0].pageX | |
this.origin.y = event.targetTouches[0].pageY | |
element.addEventListener('touchmove', this, false); | |
element.addEventListener('touchend', this, false); | |
element.addEventListener('touchcancel', this, false); | |
}, | |
touchMove: function(event){ | |
// console.debug('touchMove') | |
this.destination.x = event.targetTouches[0].pageX | |
this.destination.y = event.targetTouches[0].pageY | |
}, | |
touchEnd: function(event){ | |
// console.debug('touchEnd') | |
element.removeEventListener('touchmove', this, false); | |
element.removeEventListener('touchend', this, false); | |
element.removeEventListener('touchcancel', this, false); | |
var moved = (!!this.destination.x || !!this.destination.y); | |
// console.debug('moved: ' + moved) | |
if (moved && this.withinThreshold()) { | |
// console.debug('tapped') | |
this.callback.apply(this.element, arguments); | |
} | |
if (!moved) { | |
// console.debug('tapped'); | |
this.callback.apply(this.element, arguments); | |
} | |
this.reset(); | |
}, | |
touchCancel: function(event){ this.reset(); }, | |
reset: function(){ | |
this.origin.x = 0; | |
this.origin.y = 0; | |
this.destination.x = 0; | |
this.destination.y = 0; | |
} | |
}; | |
element.addEventListener("touchstart", element.touchHandler, false); | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment