Skip to content

Instantly share code, notes, and snippets.

@tlync
Created October 14, 2011 10:17
Show Gist options
  • Save tlync/1286743 to your computer and use it in GitHub Desktop.
Save tlync/1286743 to your computer and use it in GitHub Desktop.
tap for xui
(function(){
var hasTouch = 'ontouchstart' in window;
x$.extend({
tap: function(fn, delay){
// browser fallback
if(!hasTouch){
return this.each(function(){
x$(this).click(fn);
});
}
delay = delay || 50;
var moveAmount = 10;
return this.each(function(){
var start = 0,
x = null,
y = null;
x$(this).touchstart(function(e){
start = e.timeStamp;
var t = e.touches[0];
x = t.screenX;
y = t.screenY;
e.preventDefault();
}).touchend(function(e){
var t = e.changedTouches[0];
if((Math.abs(t.screenX - x) <= moveAmount && Math.abs(t.screenY - y) <= moveAmount) ||
e.timeStamp - start < delay){
fn.apply(this);
}
x = null;
y = null;
});
});
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment