与TouchSwipe元素上的多个滑动事件(Multiple swipe events on an

2019-11-02 17:36发布

我想绑定在DOM元素上的多个滑动事件TouchSwipe 。 该演示不具有多个事件的例子。 我试过follwing:

$("#test").swipe( {

    //Generic swipe handler for all directions
    swipeRight:function(event, direction, distance, duration, fingerCount) {
        alert("you swiped " + direction);
    },
    //Default is 75px, set to 0 for demo so any distance triggers swipe
    threshold:10
});

$("#test").swipe( {

    //Generic swipe handler for all directions
    swipeLeft:function(event, direction, distance, duration, fingerCount) {
        alert("you swiped " + direction);
    },
    //Default is 75px, set to 0 for demo so any distance triggers swipe
    threshold:10
});

http://jsfiddle.net/xktJ9/

但不幸的是,只有第一个事件触发(见小提琴)。

这可能与TouchSwipe库? 如果是的话,怎么样? 如果没有,你可以推荐一个库,能够在元素上绑定多个事件?

Answer 1:

您可以使用touchSwipe代替swipeRight或swipeLeft的swipeStatus方法。

    $("#test").swipe( {
     swipeStatus:function(event, phase, direction, distance, duration, fingers)
     {
       //Here we can check the:
       //phase : 'start', 'move', 'end', 'cancel'
       //direction : 'left', 'right', 'up', 'down' 
       //distance : Distance finger is from initial touch point in px
       //duration : Length of swipe in MS 
       //fingerCount : the number of fingers used
       if (phase=="end" && (direction == 'left' || direction == 'right')) {
         alert("you swiped " + direction);
       }
     },
     //Default is 75px, set to 0 for demo so any distance triggers swipe
     threshold:10,
     maxTimeThreshold:5000,
     fingers:'all'
   });

有关详细信息,请参阅此: 链接



Answer 2:

这是我如何添加多个滑动事件的例子。

$("#demo1").swipe( {
    //Single swipe handler for left swipes
    swipeRight:function() {
            demo1.goToPrevSlide();
                return false;
    }
    ,

    swipeLeft:function() {
            demo1.goToNextSlide();
                return false;
    },excludedElements:"",
    allowPageScroll:"vertical",
    preventDefaultEvents:false
    })


文章来源: Multiple swipe events on an element with TouchSwipe