可放置元素首选项(固定导航栏)

Droppable element preference (fixed navbar)

我已经修复了 bs4 导航栏和下面的一堆 divs。所有这些 div 包括导航栏都是可放置的,因为在这些 div 和导航栏之间拖动元素。如果从 div 拖动元素,我想优先使用导航栏。

让我试着举例说明:

让我们从导航栏中拖动元素,向下滚动一点,然后将元素拖回导航栏。元素将下降到导航栏下的 div 而不是导航栏。

<nav class="navbar fixed-top navbar-expand-lg navbar-light bg-light nav">           
    <div id="launchPad" class="pull-right">
       <div id="zarazka" style="cursor:move; z-index:1031;">
          <h6 class="h5">Zarážka</h6>          
       </div>
    </div>
</nav>     
<div id="zpravy" class="pb-3">
    <div class="row"><div class="col-12"><div class="zprava" id="1">asdasd</div></div></div>
    <div class="row"><div class="col-12"><div class="zprava" id="2">asdasd</div></div></div>
    <div class="row"><div class="col-12"><div class="zprava" id="3">asdasd</div></div></div>
    <div class="row"><div class="col-12"><div class="zprava" id="4">asdasd</div></div></div>
    <div class="row"><div class="col-12"><div class="zprava" id="5">asdasd</div></div></div>
</div>

$("nav").droppable({
    tolerance: "pointer",
    addClasses: false,                                                    
    drop: function(event, ui) { $("#launchPad").append($(ui.draggable)); }
}); 

$("#zpravy .row").droppable({
    tolerance: "pointer",
    addClasses: false,
    drop: function(event, ui) {        
        $(this).after($(ui.draggable));        
    }
});

$("#zarazka").draggable({
    cursor: "move",
    helper: 'clone',
    revert: "invalid",
    opacity: 0.5,
    zIndex: 1031,
    scroll:false,
    containment: "body"   
});   

我认为问题在于您的 nav 定位为固定的,因此它始终位于页面顶部。当你拖动你的元素时,实际上 nav 后面有几个 div,所以当你把一些东西放在 nav 上时,它被放置在 div 之间] 躺在它后面。

如果您为 nav 设置 height,并将其余内容包装在 div 中,padding-top 等于 [= nav 的 19=] 那么 nav 后面就没有任何东西了,也就不会出现混淆了。

因为这是 'dead space'(它总是隐藏在 fixed nav 后面)无论如何你都应该这样做,因为你会继续丢失 [=14 后面的内容=] 如果你不这样做。

我把主要内容包在一个<div class='main-content'></div>里,加上下面的CSS:

nav {
  min-height: 50px;
}

.main-content {
  margin-top: 50px;
}

如果这对您不起作用,请告诉我


演示

$("nav").droppable({
  tolerance: "pointer",
  addClasses: false,
  drop: function(event, ui) {
    $("#launchPad").append($(ui.draggable));
  }
});

$("#zpravy .row").droppable({
  tolerance: "pointer",
  addClasses: false,
  drop: function(event, ui) {
    $(this).after($(ui.draggable));
  }
});

$("#zarazka").draggable({
  cursor: "move",
  helper: 'clone',
  revert: "invalid",
  opacity: 0.5,
  zIndex: 1031,
  scroll: false,
  containment: "body"
});
nav {
  height: 50px;
}

.main-content {
  padding-top: 50px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js
"></script>

<nav class="navbar fixed-top navbar-expand-lg navbar-light bg-light nav">
  <div class="logo">

  </div>
  <div id="launchPad" class="pull-right">
    <div id="zarazka" style="cursor:move; z-index:1031;">
      <h6 class="h5">Zarážka</h6>
    </div>
  </div>
</nav>


<div class="main-content">

  <div id="zpravy" class="pb-3">
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="1">asdasd</div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="2">asdasd</div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="3">asdasd</div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="4">asdasd</div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="5">asdasd</div>
      </div>
    </div>
  </div>

</div>

问题的症结在于offset。当文档向下滚动时,可拖动对象将落在可放置位置之外。您可以通过查看偏移位置来欺骗它,如果拖动偏移 50px 或更小,则将其附加到 nav.

$(function() {
  var over = false;

  $(".navbar").droppable({
    tolerance: "pointer",
    addClasses: false,
    drop: function(event, ui) {
      console.log("Nav Drop: Append Drag to LaunchPad.");
      $("#launchPad").append($(ui.draggable));
    }
  });

  $("#zpravy .row").droppable({
    tolerance: "pointer",
    addClasses: false,
    drop: function(event, ui) {
      if (over) {
        console.log("Row Drop: Append Drag to LaunchPad.");
        ui.draggable.appendTo($("#launchPad"));
      } else {
        console.log("Row Drop: Append Drag after this.");
        $(this).after($(ui.draggable));
      }
    }
  });

  $("#zarazka").draggable({
    cursor: "move",
    helper: 'clone',
    revert: "invalid",
    opacity: 0.5,
    zIndex: 1032,
    scroll: false,
    containment: "body",
    drag: function(e, ui) {
      if (e.clientY < 50) {
        console.log("drag: Over set to true");
        over = true;
      } else {
        over = false;
      }
    },
    stop: function(e, ui) {
      if (e.clientY < 50) {
        console.log("dragstop: Over set to true");
        over = true;
      }
    }
  });
});
.navbar {
  min-height: 50px;
  z-index: 1031;
}

.main-content {
  padding-top: 50px;
}

.row {
  height: 40px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js
"></script>

<nav class="navbar fixed-top navbar-expand-lg navbar-light bg-light nav">
  <div class="logo">
  </div>
  <div id="launchPad" class="pull-right">
    <div id="zarazka" style="cursor:move; z-index:1031;">
      <h6 class="h5">Zarážka</h6>
    </div>
  </div>
</nav>

<div class="main-content">
  <div id="zpravy" class="pb-3">
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="1">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="2">Nullam ornare ex ac sem tempus, sit amet facilisis lacus rhoncus.</div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="3">Nullam ornare ex ac sem tempus, sit amet facilisis lacus rhoncus.</div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="4">Sed volutpat nulla a iaculis dignissim.</div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="5">Curabitur interdum mauris at elit semper commodo.</div>
      </div>
    </div>
    <div class="row">
      <div class="col-12">
        <div class="zprava" id="6">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
      </div>
    </div>
  </div>
</div>

也许不是最好的解决方案,但它会有所帮助。根据一些研究,更好的解决方案是不使用固定位置。您可以看看做一些其他的事情,但它们都是根据项目放置的位置将拖动项目附加到行或导航栏的技巧。

希望对您有所帮助。