【问题标题】:Drag and drop on first visible element拖放第一个可见元素
【发布时间】:2021-10-26 15:57:09
【问题描述】:

我尝试在可滚动区域中使用拖放操作...但我遇到了重叠问题 我创建了 2 个高度固定且可滚动的区域 我在每个区域都有孩子,并将孩子变成可拖动的 droppable

    function pageLoad() {
            for (i = 1; i < 20; i++) { $('<div id="100' + i + '" class="normal">').appendTo($("#tab1")); }
            for (i = 1; i < 20; i++) { $('<div id="200' + i + '" class="normal2">').appendTo($("#tab2")); }
            $('.normal2').draggable({
                helper: 'clone',
                cursor: 'move'
            });
            $('.normal').draggable({
                helper: 'clone',
                cursor: 'move'
            });
            $('.normal').droppable({
                tolerance: "pointer",
                hoverClass: "activeHover",
                drop: function (event, ui) {
                    var dropLocation = $(this);
                    //Due to scrollable divs, the schedule drag drop may get wrongly captured by hidden elements
                    //We ensure that the element at drop location is the same element or contained element 
                    //which captures the drop
                    //var dropElement = document.elementFromPoint(event.clientX, event.clientY);

                    //dropLocation is the droppable element on which we drop
                    //The point where we drop must be on dropLocation or its child element
                    //When the dropLocation is not visible due to scrollable div, second
                    //event which is captured by correct element is executed.  
                    //if (dropLocation == $(dropElement) || dropLocation.find($(dropElement)).length > 0) {

                    alert(ui.draggable.attr("id") + " sur " + $(this).attr("id"));

                    // }
                }
            });
            $('.normal2').droppable({
                tolerance: "pointer",
                hoverClass: "activeHover",
                drop: function (event, ui) {
                    var dropLocation = $(this);
                    //Due to scrollable divs, the schedule drag drop may get wrongly captured by hidden elements
                    //We ensure that the element at drop location is the same element or contained element 
                    //which captures the drop
                    //var dropElement = document.elementFromPoint(event.clientX, event.clientY);

                    //dropLocation is the droppable element on which we drop
                    //The point where we drop must be on dropLocation or its child element
                    //When the dropLocation is not visible due to scrollable div, second
                    //event which is captured by correct element is executed.  
                    //if (dropLocation == $(dropElement) || dropLocation.find($(dropElement)).length > 0) {

                    alert(ui.draggable.attr("id") + " sur " + $(this).attr("id"));

                    // }
                }
            });
        }
 .normal{
    background-color:aqua;
    height:30px;
    border :solid 1px black;

}
.normal2{
    background-color:blue;
    height:30px;
    border :solid 1px black;
}
 .activeHover{
     background-color:burlywood;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
 <div id="tab1" style="height:100px;overflow:scroll;">
          
      </div>
      <div id="tab2" style="height:100px;overflow:scroll;">
          
      </div>

当我在 div 上拖动 2 个 droppables 时(滚动创建一个叠加),我有 2 个警报。但我只想要可见的 droppable...

【问题讨论】:

    标签: jquery drag-and-drop overlap


    【解决方案1】:

    最后我只找到this library where jquery file filter droppable element

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 2014-06-20
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      相关资源
      最近更新 更多