【问题标题】:Place a draggable object between two droppable targets在两个可放置目标之间放置一个可拖动对象
【发布时间】:2018-11-22 12:55:47
【问题描述】:

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

我已经让它工作了,但它有点小错误。在两个放置目标显示放置状态之前,您必须在两个放置目标之间移动和重新定位对象几次。

这是我想要的结果:

我的尝试:

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>

【问题讨论】:

    标签: javascript jquery jquery-ui-draggable


    【解决方案1】:

    您可以更改 droppables 触摸的容差

    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!" );
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多