4

I'm really struggling on an Android app in Phonegap and JQuery.

All I want to do is bind a touchmove to an element and then check the X and Y coordinates as I move my finger (a drag, basically)

$('#someElm').bind('touchmove',function(event){
        //Code here..!
});

The touchmove fires when I touch the screen, but then I don't really know what the objects of the event are - I've tried event.screenX, event.pageX, but the don't work.

Any ideas?

Daniel Kurka
  • 7,973
  • 2
  • 24
  • 43
ojsglobal
  • 525
  • 1
  • 6
  • 31

1 Answers1

6

Here the reference for mobile safari (android is basically the same):

https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html

what you want is:

var x = event.touches[0].pageX;
var y = event.touches[0].pageY;

If you are running on android you also need to cancel the touchmove event to get new ones while touching. Don't ask me why...

Cœur
  • 37,241
  • 25
  • 195
  • 267
Daniel Kurka
  • 7,973
  • 2
  • 24
  • 43
  • That sort of works thanks, but it doesn't seem to get the individual positions as I drag my finger in the x directions. It only seems to give me the start position an end position. Any ideas? – ojsglobal Nov 07 '11 at 22:55
  • I don't have any more code; I've simply added your section into the touchmove event. I'm writing the x to the screen to see what the position is. I was expecting it to update the coordinates as I move my finger, but it simply gives me the start x with I touch the screen, and the final x when I release - nothing in between. – ojsglobal Nov 07 '11 at 23:05
  • is the number updating on this slider for you when you move it? http://m-gwt.com/showcase/#SliderPlace:null – Daniel Kurka Nov 07 '11 at 23:20
  • Regarding Android's touchmove bug http://uihacker.blogspot.com.es/2011/01/android-touchmove-event-bug.html – NiloVelez Oct 22 '14 at 20:46