【问题标题】:Draggable drops in wrong place on second drag第二次拖动时可拖动的位置错误
【发布时间】:2019-06-30 22:47:36
【问题描述】:

我正在尝试将“模板”(最初作为克隆)从“商店”拖到可放置的插槽中。此后,我希望能够将拖动的项目放入我希望的任何插槽中。 Draggables设置如下:

$( ".template" ).draggable({
        helper: "clone",
    }
);

$( ".item" ).draggable();

我解决了重新克隆的问题,方法是在第一次放置时将“模板”类替换为“项目”类,并在放置函数中从克隆元素切换到直接元素,如下所示:

$( ".template-slot" ).droppable({ 
accept: ".template, .item",

drop: function( event, ui ) {

    if(ui.draggable.hasClass("template")) {

        $(this).append(ui.draggable.clone())

        // add item class 
        $(".template-slot .template").addClass("item");

        // remove template class
        $(".item").removeClass("ui-draggable template");

        // expand to fit the slot
        $(".item").addClass("expand-to-slot");

        // make it draggable again
        $(".item").draggable();    

    }

    if(ui.draggable.hasClass("item")) {
        $(this).append(ui.draggable);
    }

}                            
});

第一次拖放工作正常。问题出现在第二次拖放。该元素可以拖动,但是当放下它时,无论我拖动它的方向,它都会放置两倍的距离。因此,例如,如果我将它向右拖动 50 像素,它会向右下降 100 像素。上下等也一样。

我怀疑这与偏移量累积有关,但尚未弄清楚为什么会这样做或如何解决它。

我在这里创建了一个小提琴:https://jsfiddle.net/stefem1969/a86jmnxu/10/ 来显示问题。

希望有人能提供帮助。

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-ui-draggable jquery-ui-droppable


    【解决方案1】:

    工作示例:https://jsfiddle.net/Twisty/fu83zq2y/11/

    JavaScript

    $(function() {
    
      function makeDrag(obj, opt) {
        if (obj.hasClass("ui-draggable")) {
          obj.removeClass("ui-draggable");
          obj.attr("style", "");
        }
        if (opt === undefined) {
          opt = {};
        }
        obj.draggable(opt);
      }
    
      makeDrag($(".template, .item"), {
        helper: "clone",
      });
    
      $(".template-slot").droppable({
        accept: ".template, .item",
        drop: function(event, ui) {
          if (ui.draggable.hasClass("template")) {
            console.log("it has the template class!");
            var newItem = ui.draggable.clone();
            newItem.toggleClass("template item");
            newItem.addClass("expand-to-slot");
            newItem.appendTo($(this));
            makeDrag(newItem);
          }
    
          if (ui.draggable.hasClass("item")) {
            console.log("it has the item class!")
            var droppableOffset = $(this).offset();
            console.log("droppableOffset: ", droppableOffset);
            ui.draggable.attr("style", "").appendTo($(this));
          }
        }
      });
    
      $(".template-store, .thagomizer").droppable({
        accept: ".item",
        drop: function(event, ui) {
          ui.draggable.remove();
        }
      });
    });
    

    draggable 的一部分是在移动它时设置topleft。即使您将项目附加到另一个元素,它仍然会有这些。清除可以帮助完成您正在寻找的东西。

    makeDrag() 对执行相同的操作很有帮助。

    希望这会有所帮助。

    【讨论】:

    • Twisty - 太棒了 - 非常感谢。一个问题 - 在第二次拖动之后,您在拖动元素时不再看到它。它会消失,然后在掉落时重新出现。知道是什么原因造成的吗?
    • @Stef 在第一次拖放后拖动模板时我没有看到。
    • 第二次拖拽后试试?即重新拖动已经从插槽拖到插槽的那个。我在 Windows 10 中的 chrome 和 Firefox 中进行了尝试……这不是什么大问题,但弄清楚它背后的东西会很有趣。
    • 我明白了...你需要更改:ui.draggable.attr("style", "").appendTo($(this));到 ui.draggable.attr("style", "position:relative;").appendTo($(this));因为当您将样式设置为空白时,它会丢失位置样式。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2014-10-03
    相关资源
    最近更新 更多