简体   繁体   English

jQuery draggable:只能在一个droppable上拖动

[英]jQuery draggable : draggable drag only on one droppable

I have an app with some droppable and draggable, and I want to do for one type of draggable (with a given class) this draggable can be drop only on one type of droppable. 我有一个带有一些droppable和draggable的应用程序,我想对一种类型的draggable(具有给定的类)进行操作,这种draggable只能放在一种类型的droppable上。

The problem is that it's depending of the class, at the beggining the draggable can be drag everywhere in my grid : 问题是它取决于类,在开始时可拖动对象可以在网格中的任何地方拖动:

 $('.grid-drop').droppable({
        accept: '.block',
        hoverClass: 'hovered',
        drop: handlePublishBlock
 });

So what I do is when I start to drag my element I change the accept of my grid like this : 所以我要做的是,当我开始拖动元素时,就这样改变了网格的接受度:

$('.grid-drop').droppable({
       accept: '.unknown-class',
       hoverClass: 'hovered',
       drop: handlePublishBlock
});

But it seems doesn't work, I still can drag my elem on the grid-drop. 但似乎不起作用,我仍然可以将我的元素拖到网格上。

What I want to do is that the element with this class can be drag only to one drop and after this it can't be drag another time, so here is my actual code : 我想要做的是将此类的元素只能拖到一滴,然后再拖一次,所以这是我的实际代码:

 $(".draggable-one")
        .mousedown(function() {
            $('.grid-drop').droppable({
                accept: '.unknown-class',
                hoverClass: 'hovered',
                drop: handlePublishBlock
            });

            console.log("mousedown");
            isDragging = false;
        })
        .mousemove(function() {
            console.log("mousemove");
            isDragging = true;
        })
        .mouseup(function() {
            console.log("mouseup");
            var wasDragging = isDragging;
            isDragging = false;
            if (wasDragging) {

                $('.grid-drop').droppable({
                    accept: '.block',
                    hoverClass: 'hovered',
                    drop: handlePublishBlock
                });


                $(this).draggable('disable');
                $(this).removeClass('block-draggable');
            }
        });

When I start to drag I disable the drop on my grid (but seems not work) and when I drop on the other field than the grid (there is only one other field) the element can't be drag another time. 当我开始拖动时,我禁用了在网格上的拖放(但似乎不起作用),并且当我在网格以外的其他字段(只有一个其他字段)上拖放时,该元素无法再次拖动。 This works but the disable and re-enable of the grid doesn't works. 这可以工作,但是禁用和重新启用网格不起作用。

Can anyone help me ? 谁能帮我 ? Thanks. 谢谢。

Without seeing your full code, it's hard to help. 如果没有完整的代码,很难提供帮助。 I would advise something like this: 我建议这样的事情:

$(function(){
  var $grid = $('.grid-drop').droppable({
    accept: '.block',
    hoverClass: 'hovered',
    drop: handlePublishBlock
  });
  var isDragging = false;
  $(".draggable-one").draggable({
    start: function(e, ui){
      $grid.droppable("option", "accept", ".draggable-one");
      console.log(e.type);
      isDragging = true;
    },
    stop: function(e, ui){
      $grid.droppable("option", "accept", ".block");
      console.log(e.type);
      isDragging = false;
    }
  });
});

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

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