Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

touchend event properties

If I catch all touchend events from mobile devices with:

$(document.body).bind('touchend', function (e) { var touch = e.touches[0]; // doesnt work ... 

I need to get the touch.screenX, touch.screenY, touch.clientX and touch.clientX from the e paramter. All the examples I've seen suggest that e.touches will be a collection, and you can get at the touch details with e.touches[0]. In my tests on an ipad, e.touches is always undefined. I'm not using any jquery plugins.

Also tried e.targetTouches, which is also undefined.

Can anyone help?

like image 765
Matt Roberts Avatar asked Aug 25 '11 14:08

Matt Roberts


People also ask

What is Touchend event?

The touchend event occurs when the user removes the finger from an element.

Does touch trigger click event?

When a visitor clicks on an image the click event will be triggered. However when someone touches the image, that same click event will be triggered, even if a touchstart event is available as well.

How do I use Touchmove event?

The touchmove event occurs when the user moves the finger across the screen. The touchmove event will be triggered once for each movement, and will continue to be triggered until the finger is released.

Does Safari support touch events?

Safari mobile doesn't support touch events.


2 Answers

Actually, released touches will be found in the changedTouches array, ie:

e.changedTouches[0].pageX // get the end x page coordinate for a released touch

I think this is slightly more reliable than going through the originalEvent property.

You can read more on changedTouches here: http://www.w3.org/TR/touch-events/#changedtouches-of-a-touchevent

like image 156
Gopherkhan Avatar answered Sep 20 '22 20:09

Gopherkhan


The touches property is an TouchList object. You can see the TouchList class reference here.

If you monitor its length property with this example code on #log div:

$('#element').bind('touchstart', function(event)  {     $('#log').append(event.originalEvent.touches.length+'<br/>'); });  $('#element').bind('touchmove', function(event)  {     $('#log').append(event.originalEvent.touches.length+'<br/>'); });  $('#element').bind('touchend', function(event)  {     $('#log').append(event.originalEvent.touches.length+'<br/>'); }); 

you will obtain 1 while executing touchstart and touchmove, an 0 when executing touchend. That is why you obtain undefined from e.touches[0].

like image 26
javibilbo Avatar answered Sep 23 '22 20:09

javibilbo