如何防止滑动触发点击?

时间:2013-10-07 14:31:54

标签: jquery events swipe

我使用TouchSwipe创建可滑动的图片列表。我将滑动事件绑定到图像,同时我还绑定了一个点击事件,它将打开图像的大版本。

我的问题是,如果我滑动,它也会触发click事件。我试过了tap instead of swipe,但我无法让它发挥作用。在此之后,我尝试了很多地方建议的event.preventDefault()event.stopPropagation(),但没有效果。我的最终解决方案尝试是取消绑定click事件并在事件发生后重新绑定它,但是如果我在事件函数的最后绑定事件,则会再次触发click。

$(".js-header-swipe-image").swipe({
    swipe:function(event, direction, distance, duration, fingerCount){
        $("#details").unbind('click');//Temporary unbind, otherwise the swipe's click would trigger the gallery opening.

        //Handling swipe direction.

        $('#details').on('click', '.js-header-swipe-image', function (){//Rebind the temporary unbinded event.
            console.log('click');
            $('#modal-gallery').modal('show');
        });
    }
});

有没有办法在事件结束后中止事件本身或调用函数,这样我可以在滑动完成后重新点击点击,这样就不会触发重新点击的点击?我也对这个问题的任何其他解决方案持开放态度。

4 个答案:

答案 0 :(得分:2)

我使用这段代码,这样只有在没有被刷过的情况下触发按钮(在touchend上):

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

答案 1 :(得分:1)

根据您提供给Tap vs Swipe的链接

您是否尝试过以下代码? :

$(".js-header-swipe-image").swipe({
    tap: function(event, target) {
        console.log('click');
        $('#modal-gallery').modal('show');
    },
    swipe:function(event, direction, distance, duration, fingerCount){
        //Handling swipe direction.
    }
});

编辑:工作解决方案

HTML:

<style type="text/css">
    .js-header-swipe-image {
        background-color:blue;
        color:white;
        width:500px;
        height:500px;
    }
</style>
<div id="modal-gallery">
    Hello!
</div>
<div class="js-header-swipe-image">
    Swiping Div
</div>

jQuery:

<script type="text/javascript">
    $(document).ready(function() {
        $(".js-header-swipe-image").swipe({
            tap: function(event, target) {
                alert('tap');
            },
            swipe:function(event, direction, distance, duration, fingerCount){
                //Handling swipe direction.
                alert('swipe');
            }
        });
    });
</script>

当“滑动”div时,我会收到提醒swipe,点击该div后,我会收到提醒tap

答案 2 :(得分:0)

我有同样的问题。因为我设置了threshold:0,所以不会调用tap函数。一旦我评论出来,点击事件就可以了。

            container.swipe({
                touch:function(event, target) {
                    alert('touch');
                },
                tap:function(event, target) {
                    alert('tapped');
                },
                swipeLeft:function(event, direction, distance, duration, fingerCount) {
                    console.log('swipe left');
                },
                swipeRight:function(event, direction, distance, duration, fingerCount) {
                    console.log('swipe RIGHT');
                },
                swipeUp:function(event, distance, duration, fingerCount, fingerData) {
                    console.log("swipeUp from callback");
                },
                swipeDown:function(event, distance, duration, fingerCount, fingerData) {
                    console.log("swipeDown from callback");
                }
                // threshold:0
            });

答案 3 :(得分:-1)

    var isTouchMoving = false;



    $( "#items .item" ).on("vclick", function(){
        if(isTouchMoving){
            isTouchMoving = false;
            return false;
        }

        //do something
    });

    $(document).on('vmousemove', function(){
        isTouchMoving = true;
    });

    $(document).on('scrollstop', function(){
        isTouchMoving = false;
    });