javascript - jQueryUI : how to restrict droppable area to multiple divs?

标签 javascript jquery jquery-ui draggable jquery-ui-draggable

感觉问题已经有了答案:

我有 3 个时间轴和一些可拖动的元素放置在那里。出于某种原因,我无法让它工作。这是我使用的语法:containment: ".timeline1, .timeline2, .timeline3"

enter image description here

$(function() {
  $(".timeline1, .timeline2, .timeline3").droppable();

  $(".event").draggable({
    containment: ".timeline1, .timeline2, .timeline3"
  });
});
.timeline1, .timeline2, .timeline3 {
  width: 500px;
  height: 40px;
  border: 3px dashed gray;
  margin-bottom: 10px;
}
.event {
  height: 40px;
  background: gray;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />

<div class="timeline1"></div>
<div class="timeline2"></div>
<div class="timeline3"></div>

<div class="event">dance</div>
<div class="event">sleep</div>
<div class="event">eat</div>


文档中的更新:http://api.jqueryui.com/draggable/#option-containment

The draggable element will be contained to the bounding box of the first element found by the selector

问题仍然有效,我真的很想知道如何将可放置区域限制为多个 div?

最佳答案

我不确定您是否可以使用包含选项来做到这一点,但您可以使用 snap、snapMode 和 revert 来完成您想要做的事情。

我会在您的时间轴元素中添加一个通用类,如 timeline-droppable 以使其更容易:

<div class="timeline1 timeline-droppable"></div>
<div class="timeline2 timeline-droppable"></div>
<div class="timeline3 timeline-droppable"></div>

使用捕捉选项捕捉到您的可放置时间轴。使用 revert 选项来确定 draggable 是否被放置在一个有效的 droppable 中:

$(function() {
    $(".timeline-droppable").droppable();

    $(".event").draggable({
        snap: ".timeline-droppable",
        snapMode: "inner",
        revert:  function(droppedElement) {
            var validDrop = droppedElement && droppedElement.hasClass("timeline-droppable");
            return !validDrop;
        } 
    });
});

当然,您需要调整 CSS 以使您的事件元素很好地适合您的时间轴元素。

关于javascript - jQueryUI : how to restrict droppable area to multiple divs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28387080/

相关文章:

Javascript:获取子元素的宽度

javascript - 在两个输入框中自动完成? - JavaScript

javascript - PHP 显示隐藏的 Div

javascript - 如何使用 JavaScript/jQuery 替换 URL 的路径名?

javascript - 表单未在 $.ajax 成功函数中提交

javascript - jQuery UI 1.10.3 对话框,拖拽移走对话框

javascript - 如何将可拖动父 div 的子/子 div 更改为不同的类?

javascript - 如何将 p5.js Canvas 放置在 div 容器内?

javascript - hAxis 标签位置 Google Charts

javascript - 使用 css-transitions 淡入然后在一段时间后淡出