【问题标题】:Cant use jQuery's connectWith to join sortable div不能使用 jQuery 的 connectWith 来加入可排序的 div
【发布时间】:2017-03-11 11:11:34
【问题描述】:

我正在使用 jQuery 的 sortable 函数来允许用户重新排列 div 的顺序。当用户单击按钮时,会出现一个弹出 div。

我尝试使用connectWith 函数来允许用户将此弹出式 div 拖到 div 列表中,但是它不会移动到适合它们之间 - 相反,它会停留在它们上方。这和z-index有什么关系吗?

$("#click").on("click", function() {
  $("#sortable1").show();
});

$('#sortable1, #sortable2').sortable({
  axis: 'y',
  connectWith: '.first',
});
.container {
  position: absolute;
  margin: 0 auto;
  left: 0;
  right: 0;
  top: 13%;
  width: 85%;
}

div.div {
  border: 1px solid;
  margin-top: 7.5px;
  margin-bottom: 7.5px;
}

.popup {
  width: 50%;
  left: 0;
  right: 0;
  top: 20%;
  margin: 0 auto;
  z-index: 1;
  position: absolute;
  background: #fff;
  border: 1px solid;
}
<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 id="sortable1" style="display: none;">
  <div class="popup">
    <h2>RED</h2>
    <p>When his peaceful life is threatened by a high-tech assassin, former black-ops agent Frank Moses reassembles his old team in a last ditch effort to survive and uncover his assailants.</p>
  </div>
</div>

<div class="container">

  <button id="click">Click me</button>
  <br>
  <br>

  <div id="sortable2" class="first">

    <div class="div">
      <h2>Deep Impact</h2>
      <p>Unless a comet can be destroyed before colliding with Earth, only those allowed into shelters will survive. Which people will survive?</p>
    </div>

    <div class="div">
      <h2>The Adjustment Bureau</h2>
      <p>The affair between a politician and a contemporary dancer is affected by mysterious forces keeping the lovers apart.</p>
    </div>

    <div class="div">
      <h2>Lord of the Flies</h2>
      <p>Stranded on an island, a group of schoolboys degenerate into savagery.</p>
    </div>
  </div>

</div>

JsFiddle:https://jsfiddle.net/o0exme4f/

这是预期的输出(当你拖动 div 说“拖动我”时,你可以看到它进入了说“example”的 div 之间) - 这个例子没有使用弹出:

$("#sortable1, #sortable2").sortable({
  connectWith: ".first"
});
div.ex {
  border: 1px solid;
  padding: 5px;
}
<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 id="sortable1" class="first">
  <div class="ex">Example</div>
  <div class="ex">Example</div>
  <div class="ex">Example</div>
  <div class="ex">Example</div>
</div>

<p>
  Drag this div into the set above:
</p>

<div id="sortable2">
  <div class="ex">drag me</div>
</div>

JsFiddle:https://jsfiddle.net/5ayf2yva/

【问题讨论】:

    标签: jquery html css jquery-ui jquery-ui-sortable


    【解决方案1】:

    不久前我遇到了类似的问题。这是由于您已将位置属性应用于popup。您应该从与sortable 冲突的popup css 中删除position 属性或将值更改为static。可排序库不适用于绝对定位的项目。

    .popup {
     width: 50%;
      left: 0;
      right: 0;
      top: 20%;
      margin: 0 auto;
      z-index: 1;
      background: #fff;
      border: 1px solid;
    }
    

    js fiddle

    【讨论】:

    • 啊,这似乎做到了!但是,没有position: absolute,弹出窗口的位置现在已经移动到页面顶部(top: 20% 不再工作)。有什么办法可以防止这种情况发生?
    • 另外,z-index: 1 在删除 position: absolute 时似乎不再适用,因为弹出窗口的内容位于已经存在的 div 后面。
    • @TheCodesee 哦。我会尝试寻找解决方法。
    • 我发现将position 设置为relative 可以解决z-index 问题,但仍然不能解决top 问题。
    • @TheCodesee 找到此博客 nightcrawler.ro/2014/01/… 。尝试使用这个。也许这就是你所需要的。
    猜你喜欢
    • 2011-04-29
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多