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.
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(); } });
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; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With