【问题标题】:How to prevent swipe to trigger click?如何防止滑动触发点击?
【发布时间】:2015-07-08 22:58:15
【问题描述】:

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

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

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

有没有办法中止事件本身或在事件完成后调用函数,以便我可以在滑动完成后重新绑定点击,这样它就不会触发重新绑定的点击?我也愿意接受任何其他解决问题的方法。

【问题讨论】:

  • 你找到解决办法了吗?
  • @Flakerim 不,但发现在移动平台上它的实现方式不同,所以这个问题不存在。 PC 上还是有问题。
  • 我修复了我的问题,它使用了旋转滑块滑动,我将 $.fn.swipe 重命名为 $.fn.swipeing 并调用了 .swipeing({}),检查这是否对您有帮助。您可能有其他插件可以覆盖滑动。

标签: jquery events swipe


【解决方案1】:

我使用这段代码,以便按钮只有在没有被滑动时才会触发(在触摸端):

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

【讨论】:

    【解决方案2】:

    基于您提供给 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

    【讨论】:

    • 不,甚至根本没有触发点击事件。
    • 我使用 TouchSwipe 库添加了一些对我有用的 HTML 和 Javascript。如果你不能让它与上面的代码一起工作,你能给我提供你正在使用的 jQuery 版本,以及你正在尝试的代码的JSFiddle 吗?
    【解决方案3】:

    我遇到了同样的问题。因为我设置了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
                });
    

    【讨论】:

      【解决方案4】:
          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;
          });
      

      【讨论】:

        猜你喜欢
        • 2015-04-28
        • 1970-01-01
        • 2016-06-03
        • 1970-01-01
        • 2012-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多