Determine vertical direction of a touchmove

前端 未结 5 364
名媛妹妹
名媛妹妹 2020-12-01 00:26

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:

5条回答
  •  半阙折子戏
    2020-12-01 01:00

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

提交回复
热议问题