Swipe effect in react js

后端 未结 3 727
栀梦
栀梦 2020-12-24 08:35

I\'m trying to create a swipe event using React. I do not want to use any external component or jquery.

The css is something like :

.outter{
  posit         


        
3条回答
  •  甜味超标
    2020-12-24 08:56

    I used good solution above as a base for build what I was needed. Maybe somebody need something like this. Idea was to move my custom slider left and right on swipe effect. If you want it to be more sensitive adjust 150 to 75.

    const [touchStart, setTouchStart] = React.useState(0);
    const [touchEnd, setTouchEnd] = React.useState(0);
    
    function handleTouchStart(e) {
        setTouchStart(e.targetTouches[0].clientX);
    }
    
    function handleTouchMove(e) {
        setTouchEnd(e.targetTouches[0].clientX);
    }
    
    function handleTouchEnd() {
        if (touchStart - touchEnd > 150) {
            // do your stuff here for left swipe
            moveSliderRight();
        }
    
        if (touchStart - touchEnd < -150) {
            // do your stuff here for right swipe
            moveSliderLeft();
        }
    }
    

提交回复
热议问题