Skip to content

Instantly share code, notes, and snippets.

@danielmahal
Created May 9, 2012 10:27
Show Gist options
  • Save danielmahal/2643602 to your computer and use it in GitHub Desktop.
Save danielmahal/2643602 to your computer and use it in GitHub Desktop.
Tap/Click override
if(Modernizr.touch) {
$.event.special.click = {
tapThreshold: 750,
setup: function() {
$(this).on('touchstart', $.event.special.click.touchstart);
},
teardown: function() {
$(this).off('touchstart', $.event.special.click.touchstart);
},
touchstart: function(e) {
if ((e.which && e.which !== 1) || !e.originalEvent.touches.length) return false;
var $el = $(this);
var moved = false;
var startX = e.originalEvent.touches[0].clientX;
var startY = e.originalEvent.touches[0].clientY;
var touchmove = function() {
var x = e.originalEvent.touches[0].clientX;
var y = e.originalEvent.touches[0].clientY;
if (Math.abs(x - startX) > 10 || Math.abs(y - startY) > 10) {
moved = true;
}
};
var touchend = function(e) {
if(!moved) {
e.type = "click";
$.event.handle.apply(this, arguments);
}
$el.off('touchmove', touchmove);
$el.off('touchend', touchend);
};
$el.on('touchmove', touchmove);
$el.on('touchend', touchend);
}
};
}
@danielmahal
Copy link
Author

Not currently working. Just saying…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment