Cancel click event in the mouseup event handler

前端 未结 14 2440
梦毁少年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:21

    This is my solution for drag and click on same element.

    $('selector').on('mousedown',function(){
      setTimeout(function(ele){ele.data('drag',true)},100,$(this));
    }).on('mouseup',function(){
      setTimeout(function(ele){ele.data('drag',false)},100,$(this));
    }).on('click',function(){
      if($(this).data('drag')){return;}
      // put your code here
    });
    

提交回复
热议问题