如何禁用拖动功能?

3

我希望停止在 draggable 方法中拖动功能。

$('#dragitem').draggable({
            scroll : false,
            drag: function(event, ui){
                 //a if statement
                 if(….){
                    //I want to stop the dragging here.                       
                 }
            }
 });

有没有一种方法可以停止在拖动函数内部的拖动行为?谢谢帮忙!


你试过使用 return false; 吗?尝试将其添加到该代码块中。这应该会结束该函数。 - royhowie
@Luxelin 不,它不起作用。 - FlyingCat
你解决了吗? - display-name-is-missing
3个回答

0

尝试:

$("#dragitem").draggable({ disabled: true });

0

试试这个:

$('#dragitem').draggable({
      scroll : false,
      drag: function(event, ui){
           //a if statement
           if(….){
                //I want to stop the dragging here.
                event.preventDefault();                      
           }
      }
 });

如果那不起作用,尝试 e.stopPropogation(); - royhowie
我认为你是指event.stopPropogation();吧?就我所知,你的代码中没有定义e - display-name-is-missing
为什么我们需要 event.stopPropogation()?这里没有来自父节点的事件。只需要使用 preventDefault() - ducdhm

0

我之前也遇到过这种情况,由于没有更好的解决方案,我在这里触发了 mouseup 事件:

drag: function(event, ui){
    if(true) {
        // stop dragging
        $(document).trigger('mouseup');                  
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接