Cancel click event in the mouseup event handler

前端 未结 14 2474
梦毁少年i
梦毁少年i 2020-12-09 14:53

Writing some drag&drop code, I\'d like to cancel the click events in my mouseup handler. I figured preventing default should do the trick, but the click event is still f

14条回答
  •  余生分开走
    2020-12-09 15:44

    The best solution for my situation was:

    let clickTime;
    
    el.addEventListener('mousedown', (event) => {
      clickTime = new Date();
    });
    
    el.addEventListener('click', (event) => {
      if (new Date() - clickTime < 150) {
        // click
      } else {
        // pause
      }
    });
    

    This gives the user 150ms to release, if they take longer than 150ms it's considered a pause, rather than a click

提交回复
热议问题