如果用户触摸拖动,取消touchend

时间:2014-04-10 21:21:42

标签: jquery touch drag

我在doc ready中有以下代码,用于在单击文档(背景)时隐藏菜单。这很好用。但是,如果用户开始拖动,我需要取消隐藏 - touchstart。因此,如果用户触摸并发布了#39;或者不接触,菜单隐藏。但如果他们触摸然后开始拖动以查看更多菜单,菜单将保持可见。就像现在一样,如果启动touchend,我会尝试取消touchmove,但这不起作用。它仍然隐藏菜单。这里的任何帮助将不胜感激。

var touchmove = false;
$(document).on('touchmove',function() {
    touchmove = true;
})

var click_or_touch = is_touch_divice ? 'touchend' : 'click'

$(this).on(click_or_touch, function() {

    if (touchmove == true) return false;

    if ($('.popup_item').is(':visible')) {

        $('.popup_item').hide();

    }

});

1 个答案:

答案 0 :(得分:0)

也许这可以帮到你

将变量设为false

var isDrag = false;

将var设置为true ontouchmove

$("body").on("touchmove", function(){
  isDrag = true;
});

您的按钮

$("#button").on("touchend", function(){
      if (isDrag)
      return;

      // Your button actions here; do your magic
});

重置变量

$("body").on("touchstart", function(){
    isDrag = false;
});