18

概述:

我有一个使用jquery.event.dragjquery.event.drop的页面。我需要能够拖放到不断添加到 dom 的元素上,即使在拖动开始之后也是如此。


问题:

dragstart事件触发时,它会检查可用的放置目标并将它们添加到拖动对象。

我遇到的问题是我在dragstart事件触发后动态添加放置目标,因此用户无法放置到这些动态添加的放置目标上。


例子:

http://jsfiddle.net/blowsie/36AJq/


问题:

如何更新拖动以允许拖放开始后已添加到 dom 的元素?

4

5 回答 5

5

您可以使用此代码段。

重要的功能是:$.event.special.drop.locate();

在 chrome/safari/firefox/ie9 上测试,似乎可以工作。

看演示


更新

对于重叠事件,请查看以下代码是否有效。我将它设置在一个匿名函数中只是为了避免任何全局变量。想法是使用 event 的 currentTarget 属性来检查是否不是同一个元素触发了同一个事件。我在 newdrop 元素上设置了一个 id 只是为了在这里进行测试。

查看更新的演示

(function () {
    var $body = $("body"),
        newdrops = [],
        currentTarget = {},
        ondragstart = function () {

            $(this).css('opacity', .75);
        }, ondrag = function (ev, dd) {
            $(this).css({
                top: dd.offsetY,
                left: dd.offsetX
            });
        }, ondragend = function () {

            $(this).css('opacity', '');
            for (var i = 0, z = newdrops.length; i < z; i++)
            $(newdrops[i]).off('dropstart drop dropend').removeClass('tempdrop');
            newdrops = [];
        }, ondropstart = function (e) {
            if (currentTarget.dropstart === e.currentTarget) return;
            currentTarget.dropstart = e.currentTarget;
            currentTarget.dropend = null;
            console.log('start::' + e.currentTarget.id)
            $(this).addClass("active");
        }, ondrop = function () {
            $(this).toggleClass("dropped");
        }, ondropend = function (e) {
            if (currentTarget.dropend === e.currentTarget) return;
            currentTarget.dropend = e.currentTarget;
            currentTarget.dropstart = null;
            console.log('end::' + e.currentTarget.id)
            $(this).removeClass("active");
        };

    $body.on("dragstart", ".drag", ondragstart)
        .on("drag", ".drag", ondrag)
        .on("dragend", ".drag", ondragend)
        .on("dropstart", ".drop", ondropstart)
        .on("drop", ".drop", ondrop)
        .on("dropend", ".drop", ondropend);



    var cnt = 0;
    setInterval(function () {
        var dataDroppables = $body.data('dragdata')['interactions'] ? $body.data('dragdata')['interactions'][0]['droppable'] : [];

        var $newDrop = $('<div class="drop tempdrop" id="' + cnt + '">Drop</div>');
        cnt++;
        $("#dropWrap").append($newDrop);
        var offset = $newDrop.offset();
        var dropdata = {
            active: [],
            anyactive: 0,
            elem: $newDrop[0],
            index: $('.drop').length,
            location: {
                bottom: offset.top + $newDrop.height(),
                elem: $newDrop[0],
                height: $newDrop.height(),
                left: offset.left,
                right: offset.left + $newDrop.width,
                top: offset.top,
                width: $newDrop.width
            },
            related: 0,
            winner: 0
        };
        $newDrop.data('dropdata', dropdata);
        dataDroppables.push($newDrop[0]);
        $newDrop.on("dropstart", ondropstart)
            .on("drop", ondrop)
            .on("dropend", ondropend);
        $.event.special.drop.locate($newDrop[0], dropdata.index);
        newdrops.push($newDrop[0]);
    }, 1000);
})();
于 2013-04-14T16:26:46.223 回答
1

我无法使用 jquery.event.drag 和 jquery.event.drop 让它工作,但我确实让它与原生 HTML5 事件一起工作:

http://jsfiddle.net/R2B8V/1/

解决方案是将事件绑定到函数内的放置目标上,并调用它来更新绑定。我怀疑您可以使用类似的主体与 jquery.event.drag 和 jquery.event.drop 一起使用。如果我能让那些工作,我会更新我的答案。

这是JS:

$(function() {
    var bind_targets = function() {
        $(".drop").on({
            dragenter: function() {
                $(this).addClass("active");
                return true;
            },
            dragleave: function() {
                $(this).removeClass("active");
            },
            drop: function() {
                $(this).toggleClass("dropped");
            }
        });
    };    

    $("div[draggable]").on({
        dragstart: function(evt) {
            evt.originalEvent.dataTransfer.setData('Text', 'data');
        },
        dragend: function(evt) {
            $('.active.drop').removeClass('active');   
        }
    });
  setInterval(function () {
      $("#dropWrap").append('<div class="drop">Drop</div>');
      // Do something here to update the dd.available
      bind_targets();
  }, 1000)
});
于 2013-04-13T18:57:45.863 回答
0

你不能。在 上dragstart,可能的拖放区是从 DOM 计算出来的,直到dragend. 即使不断地重新绑定.on()(演示: http: //jsfiddle.net/36AJq/84/)也不会提供预期的效果。

我以不同的方式解决了这个问题。(演示:http: //jsfiddle.net/36AJq/87/

  1. <div>从HTML 中的every 开始。
  2. 应用opacity: 0以使其不可见,并width: 0防止其dropend在隐藏时出现。
  3. 用于每 1000 毫秒setInterval显示下一个隐藏的 div ( )。$('.drop:not(.visible)').first()

JS:

$("body")
  .on("dragstart", ".drag", function () {
    $(this).css('opacity', .75);
  })
  .on("drag", ".drag", function (ev, dd) {
    $(this).css({
      top: dd.offsetY,
      left: dd.offsetX
    });
  })
  .on("dragend", ".drag", function () {
    $(this).css('opacity', '');
  })
  .on("dropstart", ".drop", function () {
    $(this).addClass("active");
  })
  .on("drop", ".drop", function () {
    $(this).toggleClass("dropped");
  })
  .on("dropend", ".drop", function () {
    $(this).removeClass("active");
  });
setInterval(function () {
    $('.drop:not(.visible)').first()
      .addClass('visible').removeClass('hidden');
}, 1000)
于 2013-04-13T17:15:18.713 回答
-1

Enable the refreshPositions option.

于 2013-04-11T22:17:58.973 回答
-2

为什么不将所有 div 放入页面并将其可见性设置为隐藏?然后使用 setInterval() 每秒更改每个人的可见性。

于 2013-04-13T16:25:50.433 回答