简体   繁体   English

如何区分点击和拖放事件?

[英]How to differentiate between click and drag/drop event?

I have a problem with element which is both draggable and also has a click event. 我有一个问题,元素既可拖动也有点击事件。

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

$('.class').click(function() {
    //...
)};

<div class="drag class"></div>

When I drag and drop the element, the click event gets fired, too. 当我拖放元素时,click事件也会被触发。 How to prevent that? 怎么预防?

Also you could probably do something with the mousemove and mousedown events together to disable the click event: 您也可以使用mousemove和mousedown事件一起执行某些操作来禁用click事件:

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;
)};

You should be able to do that by stopping the propagation on the mousedown event. 你应该能够通过停止mousedown事件的传播来做到这一点。

$('.drag').mousedown(function(event){
  event.stopPropagation();
});  

You may have to make sure that this event is attached before the click event though. 您可能必须确保在单击事件之前附加了此事件。

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');
        }
    });

I noticed that if the drag event is registered prior to click event then the problem described will not happen. 我注意到如果在单击事件之前注册了拖动事件,则不会发生所描述的问题。 Here is an example code: 这是一个示例代码:

This code create the mentioned problem: 此代码创建了上述问题:

        var that = this;
        var btnId = "button_" + this.getId();
        var minView = $("<div>", {"id":btnId, style:"position:absolute; top:"
            + this.options.style.top + ";left:" + this.options.style.left + ";border:1px solid gray;padding:2px"});
        minView.html(this.getMinimizedTitle());

        minView.click(function expendWidget(event) {
            $("#" + btnId).remove();
            that.element.css({"left":that.options.style.left, "right":that.options.style.right});
            that.element.show();
        });

        minView.draggable();
        minView.on("drag", this.handleDrag.bind(this));

        this.element.parent().append(minView);

this code does not create the problem: 此代码不会产生问题:

        var that = this;
        var btnId = "button_" + this.getId();
        var minView = $("<div>", {"id":btnId, style:"position:absolute; top:"
            + this.options.style.top + ";left:" + this.options.style.left + ";border:1px solid gray;padding:2px"});
        minView.html(this.getMinimizedTitle());

        minView.draggable();
        minView.on("drag", this.handleDrag.bind(this));

        minView.click(function expendWidget(event) {
            $("#" + btnId).remove();
            that.element.css({"left":that.options.style.left, "right":that.options.style.right});
            that.element.show();
        });
        this.element.parent().append(minView);

It's ok, but you should alway remember, that user can move mouse slightly during the click and don't notice that. 没关系,但是你应该记住,用户可以在点击过程中轻微移动鼠标而不会注意到。 So he'd think hi clicked and you – that he dragged 所以他认为嗨点击了你 - 他拖着他

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM