Vu NguyenV
Reactiflux4y ago
10 replies
Vu Nguyen

VuNguyen – 12-59 Mar 4

Hey, how do I check if user is swiping up or down using JS?

Here is my attempt:

Store the initial
touchY
in
onTouchStart
, then compare the initial
touchY
to
touchY
in
onTouchMove
. if current
touchY
is larger than initial
touchY
then it is considered that the user is swiping down, otherwise swiping up.

The problem is, if user swiping down, but decide to swipe up, it stills count as swiping down, because the current
touchY
still larger than initial
touchY


let initialTouchY = 0;

const handleTouchStart = (e) => {
      initialTouchY  = e.touches[0].clientY;
};

const handleTouchMove = (e) => {
      const currentTouchY = e.touches[0].clientY;
      
      if (currentTouchY > initialTouchY) {
        // Swiping down, but if while user swiping down suddenly swipe up, it still counts as swiping down, and this code block executed
      } else {
        // Swiping up
      }
}


How do I fix this problem. Thank you.
Was this page helpful?