Skip to content

Instantly share code, notes, and snippets.

@robbles
Created August 20, 2012 06:20
Show Gist options
  • Save robbles/3401532 to your computer and use it in GitHub Desktop.
Save robbles/3401532 to your computer and use it in GitHub Desktop.
jQuery plugin for accelerating "click" events on mobile browsers
/**
* Simple jQuery plugin that replaces $.click with a mobile-responsive version
* that doesn't break scrolling.
*/
(function($){
$.fn.extend({
clickTouch: function(handler) {
return this.each(function() {
var touchedWithoutScroll = false;
var self = $(this);
self.bind('touchstart', function(event) {
// Ignore multi-touches
if(event.originalEvent.touches.length > 1) {
return;
}
touchedWithoutScroll = true;
})
// If user starts scrolling/panning, let the touch through
.bind('touchmove', function(event) {
touchedWithoutScroll = false;
})
// If user releases without scrolling/panning/multitouching, it's a touch
.bind('touchend', function(event) {
if(touchedWithoutScroll) {
handler.apply(this, arguments);
return false;
}
})
.click(handler);
});
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment