【问题标题】:How to prevent a "longpress" touch event listener from being called for a "touch up" event as well?如何防止“长按”触摸事件侦听器也被“触摸”事件调用?
【发布时间】:2020-12-09 12:11:47
【问题描述】:

我有一个对象的以下事件监听器。

canvas.on('touch:longpress', (e) => {
    // Some Code
});

这个监听器在长按之后被调用,并且也被调用为“touch up”事件。为什么会发生这种情况?如何绕过?

【问题讨论】:

    标签: javascript events event-handling dom-events touch-event


    【解决方案1】:
      var isTouching = false;
      canvas.on('mouse:down', function (e) {
        console.log('touchstart');
        isTouching = true;
      });
      canvas.on('touch:longpress', function (e) {
        if (isTouching) {
          // Some Code
          console.log('longpress');
        }
      });
      canvas.on('mouse:up', function (e) {
        console.log('touchend');
        isTouching = false;
      });
    

    你可以用一个布尔变量来解决这种情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-27
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多