Skip to content

Instantly share code, notes, and snippets.

@tlync
Created May 18, 2012 11:23
Show Gist options
  • Save tlync/2724765 to your computer and use it in GitHub Desktop.
Save tlync/2724765 to your computer and use it in GitHub Desktop.
jquery-fastclick.js
(function($){
var hasTouch = 'ontouchstart' in window
$.fn.fastclick = function(fn, delay, amount){
delay = delay || 50
amount = amount || 10
// browser fallback
if(!hasTouch){
return this.each(function(){
$(this).click(fn)
})
}
return this.each(function(){
var s = 0,
x = null,
y = null
$(this).bind('touchstart', function(ev){
var e = ev.originalEvent,
t = e.touches[0]
x = t.screenX
y = t.screenY
//e.preventDefault()
}).bind('touchend', function(ev){
var e = ev.originalEvent,
t = e.changedTouches[0]
if((Math.abs(t.screenX - x) <= amount &&
Math.abs(t.screenY - y) <= amount) ||
e.timeStamp - s < delay){
fn.apply(this)
}
x = null
y = null
})
})
}
})(jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment