Skip to content

Instantly share code, notes, and snippets.

@Prinzhorn
Created April 4, 2013 21:04
Show Gist options
  • Save Prinzhorn/5314333 to your computer and use it in GitHub Desktop.
Save Prinzhorn/5314333 to your computer and use it in GitHub Desktop.
When preventing touch events from their default action, you can't click links or form elements. By checking if the move distance was small (i.e. it was just a tap) we can trigger a click on this element.
//Check if it was more like a tap.
var distanceY = initialTouchY - currentTouchY;
var distanceX = initialTouchX - currentTouchX;
var distance2 = distanceX * distanceX + distanceY * distanceY;
//Why use Math.sqrt when you can just compare the square number ;-).
if(distance2 < 49) {
//It was a tap, grab the element and click it.
var elementToClick = document.elementFromPoint(initialTouchX, initialTouchY);
elementToClick.click();
return;
}
@cubiq
Copy link

cubiq commented Apr 5, 2013

iscroll creates a mouseEvent on the fly that theoretically should be better than .click(). Have to try that.

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