将可拖动对象放在两个可放置目标之间

时间:2018-11-22 12:55:47

标签: javascript jquery jquery-ui-draggable

我正在尝试创建一个拖放操作,以允许将可拖动对象放置在两个可放置目标之间。两个可放置目标都必须同时接收和接受可拖动对象,并更新其“已拖放!”状态。

我已经使它工作了,但它有一些问题。您必须在两个放置目标之间移动对象并将其重新放置几次,然后它们才能显示放置状态。

这是我想要的结果:

enter image description here

我的尝试:

https://jsfiddle.net/9Lynb2s6/

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Droppable - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <style>
  #draggable { width: 100px; height: 100px;}
  #droppable { width: 150px; height: 150px; float: left; margin: 10px; }
  #droppablea { width: 150px; height: 150px; float: left; margin: 10px; }
  </style>
  <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>
  <script>
  $( function() {
    $( "#draggable" ).draggable();
        $( "#droppablea" ).droppable({
      drop: function( event, ui ) {
        $( this )
          .addClass( "ui-state-highlight" )
          .find( "p" )
            .html( "Dropped!" );
      }
    });
    $( "#droppable" ).droppable({
      drop: function( event, ui ) {
        $( this )
          .addClass( "ui-state-highlight" )
          .find( "p" )
            .html( "Dropped!" );
      }
    });
  } );
  </script>
</head>
<body>

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


<div id="droppable" class="ui-widget-header">
  <p>Drop here</p>
</div>

<div id="droppablea" class="ui-widget-header">
  <p>Drop here</p>
</div>


</body>
</html>

1 个答案:

答案 0 :(得分:1)

您可以更改可触摸物体的容忍度

http://api.jqueryui.com/droppable/#option-tolerance

$( "#droppablea" ).droppable({
  tolerance: "touch",
  drop: function( event, ui ) {
    $( this )
      .addClass( "ui-state-highlight" )
      .find( "p" )
        .html( "Dropped!" );
  }
});
$( "#droppable" ).droppable({
  tolerance: "touch",
  drop: function( event, ui ) {
    $( this )
      .addClass( "ui-state-highlight" )
      .find( "p" )
        .html( "Dropped!" );
  }
});