Created
April 4, 2013 21:04
-
-
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.
This file contains hidden or 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
//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; | |
} |
and focus()
and blur()
on touchstart
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
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?