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;
}
@Prinzhorn
Copy link
Author

why elementFromPoint? you have the element in the event?

hm, you're right. Originally I wanted something like a pointer event polyfill, but more dynamic. Not sure if I still need it, my mind was doing crazy things in the past 24 hours.

But anyway: I think the key is to call click(). I remember iScroll having problems with passing those along. There was this form inputs example with the regular expression checking for tagName (select, input etc.).

You've done much more touch event stuff then I did, so is this a valid solution?

@Prinzhorn
Copy link
Author

and focus()

and blur() on touchstart

@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