Determine vertical direction of a touchmove

madprops picture madprops · Nov 7, 2012 · Viewed 58.4k times · Source

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.

Answer

Aureliano Far Suau picture Aureliano Far Suau · Mar 7, 2014

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();
   }
});