jQuery拖动与碰撞检测

Jim*_*ery 6 javascript jquery collision-detection jquery-ui-draggable

我有以下HTML:

<div class="list" id="list">
    <div class="item" id="i1">Item 1</div>
    <div class="item" id="i2">Item 2</div>
    <div class="item" id="i3">Item 3</div>
</div>
<div class="timeline" id="timeline">
</div>
Run Code Online (Sandbox Code Playgroud)

我想用jQuery做的是:

  1. 能够将.items从中#list拖入#timeline
  2. .items可以根据需要多次放入时间线,例如.#i1时间轴中可能有4个项目.
  3. .item时间轴中的s不得相互重叠
  4. .items可以定位在时间轴上的任何位置,只要它们不与时间轴上的任何其他项重叠

所以我去了jQueryUI的Draggable和Droppable,也去了jQueryUI Draggable Collision插件.

这是我开始使用的jQuery:

$('#list .item').draggable({
    helper: 'clone',
    revert: 'invalid',
    //the following are for the jquery-ui-dragggable-collision plugin
    obstacle: '#timeline .item',
    preventCollision: true
});
$('#timeline').droppable({
    accept: '.item'
});
Run Code Online (Sandbox Code Playgroud)

我的问题是jQueryUI可拖动碰撞插件仅在拖动原始Div本身而不拖动帮助程序时才有效.我需要助手才能达到#2(添加一个项目的多个副本).但是我需要像Collision Plugin这样的东西,所以我可以实现#3(项目不重叠).

有人知道这个问题的解决方案吗?是否有另一个插件可以对可拖动对象的助手进行碰撞检测?还有另一种方法我可以尝试获得我想要实现的目标吗?

Ed *_*yed 1

这是我为此问题编写的一个示例,显示了一个具有碰撞检测功能的简单拖放插件。它允许您将项目拖放到时间轴上,只要项目有空间存在而不重叠即可。

它绝不是一个成品,但希望能表明这样的代码编写起来并不复杂,并且尝试将大量冲突的插件组合在一起并不总是最好的选择。有时最好从头开始。它很有趣,也是一种非常好的学习方式。

/*----------ON DOCUMENT READY----------*/

$(document).ready(function() {

  $("#timeline").timeline({
    items: ".item"
  });

});

/*----------THE TIMELINE PLUGIN----------*/

$.fn.timeline = function(options) {

  var defaults = {
    items: "div"
  }

  var options = $.extend(defaults, options)

  return this.each(function() {

    //-----SETUP-----//

    //define all the vars we will need later
    var el = $(this);
    var items = $(options.items);
    var mousedown = false;
    var dragging = false;
    var activeItem = false;
    var placedItems = new Array();

    //make everything unselectable so it dosne interfere with dragging
    $("html").find("*").css({
      "user-select": "none",
      "-moz-user-select": "none",
      "-webkit-user-select": "none",
      "-ms-user-select": "none",
      "-o-user-select": "none",
    }).attr("unselectable", "true").unbind("onselectstart");

    //-----EVENTS-----//

    //log when the mouse is down anywhere on the doc
    $(document).mousedown(function() {
      mousedown = true;
    });

    //when the mouse is released
    $(document).mouseup(function(e) {
      //if was dragging an item attempt to place it
      if (mousedown && dragging) {
        placeItem(e);
      }
      //log that dragging has stopped
      mousedown = false;
      dragging = false;
    });

    //log when the mouse is pressed over an item
    items.mousedown(function() {
      dragging = true;
      //clone the active item and hide it ready for dragging
      activeItem = $(this).clone().appendTo("body").hide();
    });

    //when the mouse movers over the doc
    $(document).mousemove(function(e) {
      //if mouse was pressed over item attempt to drag
      if (mousedown && dragging) {
        dragItem(e);
      }
    });

    //-----FUNCTIONS-----//

    //drag the item around the screen
    function dragItem(e) {

      //if no active item done do owt
      if (!activeItem) {
        return false;
      }

      //work out where the drag anchor is
      var x = e.pageX - (activeItem.height() / 2);
      var y = e.pageY - (activeItem.width() / 2);

      //save the original position in case we cant place the item
      if (!activeItem.origPos) {
        activeItem.origPos = {
          x: x,
          y: y
        }
      }

      //drag the item
      activeItem.css({
        "position": "absolute",
        "top": y,
        "left": x,
        "z-index": "999",
        "opacity": 0.6,
        "display": "block"
      });

    }

    //attempt to place the item
    function placeItem(e) {

      //if no active item dont do owt
      if (!activeItem) {
        return false;
      }

      //define som vars needed later on
      var onTargetY = false;
      var onTargetX = false;
      var remove = false;
      var collision = false;

      //check if item is being relesed withing the timeline bounds
      if (e.pageY > el.position().top && e.pageY < el.position().top + el.height()) {
        onTargetY = true;
      }
      if (e.pageX > el.position().left && e.pageX < el.position().left + el.width()) {
        onTargetX = true;
      }

      //if on target attempt to drop on timeline
      if (onTargetX && onTargetY) {

        //snap to the left or right if dropped at the left or right edges
        var maxLeft = el.position().left;
        var maxRight = el.position().left + el.width() - activeItem.width();
        x = e.pageX - (activeItem.width() / 2);
        if (x < maxLeft) {
          x = maxLeft;
        } else if (x > maxRight) {
          x = maxRight;
        }

        //loop the items already on the timeline and check for collisions
        $.each(placedItems, function(i, item) {
          var itemMin = item.position().left;
          var itemMax = item.position().left + item.width();
          if (x + activeItem.width() > itemMin && x < itemMax) {
            collision = true;
          }
        });
        y = el.position().top;

      }

      //if there is a collision or the item is dropped outside the timeline 
      //set x and y back to original position and set removal flag to true
      if (collision || !onTargetX || !onTargetY) {
        x = activeItem.origPos.x;
        y = activeItem.origPos.y;
        remove = true;
        //if dropped inside the timeline and no collisions add item to the 
        //array of items inside the timeline
      } else {
        placedItems.push(activeItem);
      }

      //finally either animate the item back to where it started then remove it
      //or snap it into the timeline in the space found
      activeItem.animate({
        top: y,
        left: x
      }, {
        duration: 300,
        queue: false,
        complete: function() {

          //if remove flag set remove the item from the dom
          if (remove) {
            $(this).remove();
          }

          //some tidying up
          activeItem.css("opacity", 1);
          activeItem = false;

        }
      });

    }

  });

}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="list" id="list">
  <div class="item item1">Item 1</div>
  <div class="item item2">Item 2</div>
  <div class="item item3">Item 3</div>
</div>

<div class="timeline" id="timeline"></div>
Run Code Online (Sandbox Code Playgroud)

享受 :)。