How to differentiate between click and drag/drop event?

前端 未结 5 953
梦毁少年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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-16 00:14

    Also you could probably do something with the mousemove and mousedown events together to disable the click event:

    var dragging = 0;
    
    $('.drag').mousedown(function() {
        $(document).mousemove(function(){
           dragging = 1;
        });
    });
    
    $(document).mouseup(function(){
        dragging = 0;
        $(document).unbind('mousemove');
    });
    
    $('.class').click(function() {
        if (dragging == 0){
           // default behaviour goes here
        }
        else return false;
    )};
    

提交回复
热议问题