Determine vertical direction of a touchmove

前端 未结 5 362
名媛妹妹
名媛妹妹 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 00:59

    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.

     var y = 0; //current y pos
     var sy = y; //previous y pos
     var error = 5; //touch sensitivity, I found between 4 and 7 to be good values. 
    
     function move(e) {
        //get current y pos
        y = e.pageY;
    
        //ingnore user jitter
        if (Math.abs(y - sy) > error) {
            //find direction of y
            if (y > sy) {
                //move down 
            } else {
                //move up
            }
            //store current y pos
            sy = y;
        }
    }
    

提交回复
热议问题