prevent touchstart when swiping

前端 未结 11 2073
萌比男神i
萌比男神i 2020-11-30 20:09

I have a scrollable list on a mobile device. They want people to be able to scroll the list via swiping, and also select a row by tapping.

The catch is combining th

11条回答
  •  猫巷女王i
    2020-11-30 20:59

    I use this bit of code so that buttons are only triggered (on touchend) if not being swiped on:

    var startY;
    var yDistance;
    
    function touchHandler(event) {
        touch = event.changedTouches[0];
        event.preventDefault();
    }
    
    $('.button').on("touchstart", touchHandler, true);
    $('.button').on("touchmove", touchHandler, true);
    
    $('.button').on("touchstart", function(){
        startY = touch.clientY;
    });
    
    $('.button').on('touchend', function(){
    
        yDistance = startY - touch.clientY;
    
        if(Math.abs(yDist) < 30){
    
            //button response here, only if user is not swiping
            console.log("button pressed")
        }
    });
    

提交回复
热议问题