How to differentiate between click and drag/drop event?

前端 未结 5 949
梦毁少年i
梦毁少年i 2021-02-15 23:23

I have a problem with element which is both draggable and also has a click event.

$(\'.drag\').mousedown(function() {
    //...
});

$(\'.class\').click(function         


        
5条回答
  •  鱼传尺愫
    2021-02-16 00:11

    In my case selected answer didn't worked. So here is my solution which worked properly(may be useful for someone):

        var dragging = 0;
        $(document).mousedown(function() {
            dragging = 0;
            $(document).mousemove(function(){
               dragging = 1;
            });
        });
    
        $('.class').click(function(e) {
            e.preventDefault();
            if (dragging == 0){
                alert('it was click');
            }
            else{
                alert('it was a drag');
            }
        });
    

提交回复
热议问题