jQuery UI draggable: 放置后在网格中的位置

jQuery UI draggable: position in grid after drop

绑定时使用grid: [90, 83]使元素仅在指定的网格中移动。我试图让它自由移动(因为它没有设置 grid)然后,当用户停止拖动元素时,将它放在一个网格中。

例如,在您的 Android 智能手机上,当您重新排列主屏幕上的图标时,您可以将它们拖到任何您喜欢的地方,松开后它们会粘在一个网格上。

到目前为止,我尝试了以下 JS 代码:

$(".home-item.desktop-icon:last-child").draggable({
    //grid: [90, 83],
    containment: "parent",
    stop: function (event, ui) {
        y = ui.position.top,
        x = ui.position.left,
        _this = this;
        if ((x - 10)%83 >= 42) {
            posX(x-(x%83) + 93);
        }
        else {
            posX(x-(x%83) + 10);
        }
        if ((y - 10)%90 >= 45) {
            posY(y-(y%90) + 100);
        }
        else {
            posX(y-(y%90) + 10);
        }
        function posX (f) {
            $(_this).css("top", f + "px");
        }
        function posY (f) {
            $(_this).css("left", y + "px");
        }
    }
});

不知道为什么,但是,这根本不起作用。我该怎么办?

这是一个基于我的评论的示例。

$(function() {
  $(".draggable").draggable({
    containment: "parent"
  });
  $("#snaptarget").droppable({
    accept: ".draggable",
    drop: function(e, ui) {
      var cPPos = ui.position;
      var cOPos = ui.offset;
      var cPosOff = {
        top: Math.round(cOPos.top) % 90,
        left: Math.round(cOPos.left) % 83
      };
      var sPos = {
        top: Math.round(cOPos.top),
        left: Math.round(cOPos.left)
      };
      console.log("Dropped", cPPos, cOPos, cPosOff, sPos);
      var $item = ui.draggable;
      if (cPosOff.top > 0 && cPosOff.top < 70) {
        sPos.top = sPos.top - cPosOff.top;
        console.log("DROP - TOP: " + cOPos.top + " lower to " + sPos.top);
      } else {
        sPos.top = sPos.top + (90 - cPosOff.top);
        console.log("DROP - TOP: " + cOPos.top + " rise to " + sPos.top);
      }
      if (cPosOff.left > 0 && cPosOff.left < 61) {
        sPos.left = sPos.left - cPosOff.left;
        console.log("DROP - LEFT: " + cOPos.left + " left to " + sPos.left);
      } else {
        sPos.left = sPos.left + (83 - cPosOff.left);
        console.log("DROP - LEFT: " + cOPos.left + " right to " + sPos.left);
      }
      $item.appendTo($(this)).css({
        margin: 0,
        position: "absolute",
        top: sPos.top + "px",
        left: sPos.left + "px"
      });
    }
  });
});
.drag-area {
  min-height: 300px;
}

.draggable {
  width: 80px;
  height: 80px;
  display: inline-block;
  margin: 0 10px 10px 0;
  font-size: .9em;
}

.ui-widget-header p,
.ui-widget-content p {
  margin: 0;
}

#snaptarget {
  height: 173px;
  position: relative;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div class="drag-area">
  <div id="snaptarget" class="ui-widget-header">
  </div>

  <br style="clear:both">

  <div id="draggable" class="draggable ui-widget-content">
    <p>A</p>
  </div>

  <div id="draggable2" class="draggable ui-widget-content">
    <p>B</p>
  </div>

  <div id="draggable3" class="draggable ui-widget-content">
    <p>C</p>
  </div>

  <div id="draggable4" class="draggable ui-widget-content">
    <p>D</p>
  </div>

  <div id="draggable5" class="draggable ui-widget-content">
    <p>E</p>
  </div>
</div>

我留下了一些未清理的内容,以便您在控制台中获得更多反馈。拖动时,您需要更全面的运动范围,但当用户放下项目时,您希望它捕捉到一个位置。

最初,我想要 50% 的容忍度,但发现这太贪心了,所以我尝试了大约 75% 的容忍度。例如,如果用户放下项目并且它的 top,left{12, 140},您可能希望它对齐到 {0,83}。如果用户懒惰或不知道它会捕捉,他们可能会将它拖近,以达到下一个目标,我的代码将对右侧的下一个块有 ~25% 的容差。因此,如果它们落在 {12,164},它会滑到更靠近 {0,166}.

的正确方块

希望对您有所帮助。