skip to Main Content

How do i detect scroll direction / touch direction without actually scrolling on mobile and desktop?

I am currently using the below, but as we know this doesn’t work on mobile.

window.onwheel = e => {
  if(e.deltaY >= 0){ //down

  } else { //up

  }
}

2

Answers


  1. Chosen as BEST ANSWER

    Resolved - I had this inside of an intersection observer and it turns out it wasn't detecting the element because the threshold was too high.


  2. Try using touchstart and touchmove events. Here is an example:

    let startY;
    
    window.addEventListener('touchstart', e => {
      startY = e.touches[0].clientY;
    });
    
    window.addEventListener('touchmove', e => {
      const deltaY = e.touches[0].clientY - startY;
    
      if (deltaY >= 0) {
        // Downward scroll/touch direction
      } else {
        // Upward scroll/touch direction
      }
    });
    

    However, you might have to adjust the sensitivity of the touch movement threshold for it to work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search