Javascript 使用TouchSwipe在一个元素上执行多个滑动事件

Javascript 使用TouchSwipe在一个元素上执行多个滑动事件,javascript,jquery,touch,Javascript,Jquery,Touch,我想用绑定一个DOM元素上的多个滑动事件。没有包含多个事件的示例。我试过以下方法: $("#test").swipe( { //Generic swipe handler for all directions swipeRight:function(event, direction, distance, duration, fingerCount) { alert("you swiped " + direction); }, //Default

我想用绑定一个DOM元素上的多个滑动事件。没有包含多个事件的示例。我试过以下方法:

$("#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
});

但不幸的是,只有第一个事件触发(参见fiddle)


TouchWipe库是否可以实现这一点?如果是,如何进行?如果没有,您能推荐一个能够在一个元素上绑定多个事件的库吗?

您可以使用touchSwipe的swipeStatus方法,而不是swipeRight或swipeLeft

    $("#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'
   });

有关更多详细信息,请参见:

这是如何添加多个刷卡事件的示例

$("#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
    })

而不是添加事件。。。您可以使用
方向
参数检查
方向
是的,我现在就是这样做的。但不幸的是,这些事件必须动态添加/删除。我可以编写自己的机制来注册/注销事件,但我更希望由库来处理。@leo,有什么解释吗?