掉落的物品掉出盒子

dropped item going outside of the box

我正在尝试使用 jquery UI draggable and droppable 进行拖放,它正在工作,但是有拖放部分,但是当我释放活动选项卡上的点击时,li 是走到盒子的尽头

something like this is happening how can I fix this?

代码部分: html:

<div class="card-body" id="divset">
               <div class="defaults">
                <div class="heading"><h4>Default Fields</h4></div>
                <div class="defaultItems">
                    <ul id="source">
                        @foreach($structs as $struct)
                            <li data-id="{{ $struct->id }}" data-value="inps">{{ $struct->label }}</li>
                        @endforeach
                    </ul>
                </div>
                
               </div>
               <div class="activeInp" id="activeInp">
                    <h4>Active Input Fields</h4>
                    <ul class="items" id="items">

                    </ul>
               </div>
               
            </div> <!-- end card body-->

Css:

.defaults{
    font-size: 20px;
    width: 50%;
    display: inline-block;
    flex-direction: row;
    padding: 5px;
    border: 1px solid darkblue;
}
.defaults li{
    border: 1px solid black;
    background: #BDBEBD;
    color:black;
    padding: 5px;
}
.activeInp{
    flex-direction: row;
    padding: 5px;
}
#divset{
    display: flex;
    padding: 15px;
    height: 30rem;
}

.heading{
    letter-spacing: 1px;
    font-size: 15px;
    height: 10px;
    flex-direction: row;
    width: 100%;
}
.defaultItems{
    flex-direction: row;
    width: 100%;
    padding: 25px;
}

.activeInp li{
    border: 1px solid black;
    background: #BDBEBD;
    color:rgb(6, 128, 22);
    font-weight: 700;
    padding: 5px;
}
.activeInp{
    font-size: 20px;
    width: 50%;
    display: inline-block;
    flex-direction: row;
    padding: 5px;
    border: 1px solid darkcyan;
    justify-content: left;

}

Jquery:

var ids = [];
    $('#source li').draggable({
        revert : function(event, ui) {
            // on older version of jQuery use "draggable"
            // $(this).data("draggable")
            // on 2.x versions of jQuery use "ui-draggable"
            // $(this).data("ui-draggable")
            $(this).data("uiDraggable").originalPosition = {
                top : 0,
                left : 0
            };
            // return boolean
            return !event;
            // that evaluate like this:
            // return event !== false ? false : true;
        }
    });
    $('#activeInp').droppable({
        drop:function(event,ui){
            accept:'li[data-value="inps"]'
            $('#items').append(ui.draggable);
            ids.push(ui.draggable.attr("data-id"));
            
        }
    });

任何帮助将不胜感激

请考虑以下内容,稍作改动:

$(function() {
  var ids = [];
  $('#source li').draggable({
    revert: "invalid"
  });
  $('#activeInp').droppable({
    accept: 'li[data-value="inps"]',
    drop: function(event, ui) {
      $('#items').append(ui.draggable.attr("style", ""));
      ids.push(ui.draggable.attr("data-id"));
    }
  });
});
.defaults {
  font-size: 20px;
  width: 50%;
  display: inline-block;
  flex-direction: row;
  padding: 5px;
  border: 1px solid darkblue;
}

.defaults li {
  border: 1px solid black;
  background: #BDBEBD;
  color: black;
  padding: 5px;
}

.activeInp {
  flex-direction: row;
  padding: 5px;
}

#divset {
  display: flex;
  padding: 15px;
  height: 30rem;
}

.heading {
  letter-spacing: 1px;
  font-size: 15px;
  height: 10px;
  flex-direction: row;
  width: 100%;
}

.defaultItems {
  flex-direction: row;
  width: 100%;
  padding: 25px;
}

.activeInp li {
  border: 1px solid black;
  background: #BDBEBD;
  color: rgb(6, 128, 22);
  font-weight: 700;
  padding: 5px;
}

.activeInp {
  font-size: 20px;
  width: 50%;
  display: inline-block;
  flex-direction: row;
  padding: 5px;
  border: 1px solid darkcyan;
  justify-content: left;
}
<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="card-body" id="divset">
  <div class="defaults">
    <div class="heading">
      <h4>Default Fields</h4>
    </div>
    <div class="defaultItems">
      <ul id="source">
        <li data-id="item-1" data-value="inps">Item 1</li>
        <li data-id="item-2" data-value="slct">Item 2</li>
        <li data-id="item-3" data-value="inps">Item 3</li>
        <li data-id="item-4" data-value="inps">Item 4</li>
        <li data-id="item-5" data-value="inps">Item 5</li>
      </ul>
    </div>
  </div>
  <div class="activeInp" id="activeInp">
    <h4>Active Input Fields</h4>
    <ul class="items" id="items"></ul>
  </div>
</div>
<!-- end card body-->

您会看到我已经删除了元素样式。这是通过拖动更新它的位置。这将确保用户可以放下项目并删除所有样式:

$('#items').append(ui.draggable.attr("style", ""));

您可能还想看看 Sortable Connected Lists,因为这样可以更轻松地完成您想要的操作。