Created
August 20, 2012 06:20
-
-
Save robbles/3401532 to your computer and use it in GitHub Desktop.
jQuery plugin for accelerating "click" events on mobile browsers
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
/** | |
* 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