【问题标题】:Enable droppable element to accept draggables only when it is empty使可放置元素仅在为空时才接受可拖动元素
【发布时间】:2014-12-22 07:40:48
【问题描述】:

在这个jsFiddle 中,我遇到了一个问题。让我描述一下

当我将任何包含“N”的元素拖到其他 <td> 时,它运行良好。但是在那之后,当我尝试将任何元素拖动到它的原始位置时,它就不起作用了。

我的期望:当任何元素移动到其他地方时,必须允许旧地方接受其他元素。

以下是代码:

$(function () {
  $(".dragDiv").draggable({
    create: function () {
        $(this).data('position', $(this).position())
    },
    revert: 'invalid',
    stop: function () {
        $(this).draggable('option', 'revert', 'invalid');
    }
  });
  $('.dragDiv').droppable({
    greedy: true,
    tolerance: 'touch',
    drop: function (event, ui) {
        ui.draggable.draggable('option', 'revert', true);
    }
  });
  $(".mainTable tr td").droppable({
    drop: function (event, ui) {
        console.log(JSON.stringify($(this).attr('id')));
        console.log(JSON.stringify(ui.draggable.attr("id")));
        snapToMiddle(ui.draggable, $(this));
    },
    accept: function () {
        return $(this).find("*").length == 0;
    }
  });
});
function snapToMiddle(dragger, target) {
  var topMove = target.position().top - dragger.data('position').top + (target.outerHeight(true) - dragger.outerHeight(true)) / 2;
  var leftMove = target.position().left - dragger.data('position').left + (target.outerWidth(true) - dragger.outerWidth(true)) / 2;
  dragger.animate({
    top: topMove,
    left: leftMove
  }, {
    duration: 100,
    easing: 'easeOutBack'
  });
}

【问题讨论】:

    标签: jquery jquery-ui drag-and-drop jquery-ui-draggable jquery-ui-droppable


    【解决方案1】:

    问题是 jQuery UI 可拖动小部件实际上并没有将元素附加到 droppable 上,它只是操纵 CSS 定位属性,因此即使项目被拖放到另一个 droppable 中,$(this).find("*").length == 0; 也会为 false。

    您可以通过手动将 draggable 附加到 droppable 来解决此问题。

    此外,您可以使用position() 实用方法轻松地将可拖动对象相对于可放置对象居中。


    $(function() {
      $(".dragDiv").draggable({
        revert: 'invalid'
      });
      $(".mainTable tr td").droppable({
        accept: function() {
          return $(this).find("*").length == 0;
        },
        drop: function(event, ui) {
          var $this = $(this);
          $this.append(ui.draggable.css({
            /* manually append the element
                    and reset positioning */
            top: 0,
            left: 0
          }));
          ui.draggable.position({
            my: "center",
            at: "center",
            of: $this,
            using: function(pos) {
              $(this).animate(pos, 100, "easeOutBack");
            }
          });
        }
      });
    });
    .mainTable {
      margin: 20px auto;
      padding: 0px;
    }
    .mainTable tr td {
      width: 100px;
      height: 100px;
    }
    .dragDiv {
      text-align: center;
      background-color: #00ABA9;
      height: 50px;
      vertical-align: middle;
      margin: auto;
      position: relative;
      width: 50%;
    }
    <link href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
    <table border="1" class="mainTable">
      <tr>
        <td id="11">
          <div class="dragDiv" id="1">N</div>
        </td>
        <td id="12">&nbsp;</td>
        <td id="13">&nbsp;</td>
      </tr>
      <tr>
        <td id="21">&nbsp;</td>
        <td id="22">
          <div class="dragDiv" id="2">N</div>
        </td>
        <td id="23">&nbsp;</td>
      </tr>
      <tr>
        <td id="31">&nbsp;</td>
        <td id="32">&nbsp;</td>
        <td id="33">
          <div class="dragDiv" id="3">N</div>
        </td>
      </tr>
    </table>

    【讨论】:

    • 非常感谢。你给了我想要的解决方案
    • 如何设置“ ”拖走子元素后的可放置元素?
    • @Napster 你不能在td 中默认放一个&amp;nbsp 吗?听起来像是xy 问题。也许这是可以通过 css 修复的东西..?
    猜你喜欢
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多