Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine vertical direction of a touchmove

Tags:

jquery

ipad

touch

i'm trying to implement a touch listener for tablets to trigger some actions depending whether it touchmoved upwards or downwards.

I tried the native listener:

($document).bind('touchmove', function (e) {     alert("it worked but i don't know the direction"); }); 

But i don't know how to determine the direction.

Is this possible?

Or do i need to use touchstart/touchend, if I need this can I determine the direction before the touch movement stops?

If I can only do this with an external library, what's the best one?

thanks.

like image 361
madprops Avatar asked Nov 07 '12 20:11

madprops


2 Answers

I had some issues in Ipad and solved it with two events

var ts; $(document).bind('touchstart', function (e){    ts = e.originalEvent.touches[0].clientY; });  $(document).bind('touchend', function (e){    var te = e.originalEvent.changedTouches[0].clientY;    if(ts > te+5){       slide_down();    }else if(ts < te-5){       slide_up();    } }); 
like image 99
Aureliano Far Suau Avatar answered Sep 19 '22 11:09

Aureliano Far Suau


You need to save the last position of the touch, then compare it to the current one.
Rough example:

var lastY; $(document).bind('touchmove', function (e){      var currentY = e.originalEvent.touches[0].clientY;      if(currentY > lastY){          // moved down      }else if(currentY < lastY){          // moved up      }      lastY = currentY; }); 
like image 45
Andy Avatar answered Sep 19 '22 11:09

Andy