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.
You need to save the last position of the touch, then compare it to the current one.
Rough example:
I have created a script that will ensure an element within your document will scroll without scrolling anything else, including the body. This works in a browser as well as a mobile device / tablet.
This solution takes into account change in directions which the current answers does not. The solution below also takes care of touch sensitivity; this when the user is moving in one direction but on touch end the users finger nudges in a different direction messing up the actual direction.
I had some issues in Ipad and solved it with two events
Aureliano's answer seems to be really accurate, but somehow it didn't work for me, so giving him the credits I decided to improve his answer with the following:
I simply changed the
'touchend'
event for'touchmove'