【问题标题】:Draggable elements to stack into droppable container(bottom to top)可拖动元素堆叠到可放置容器中(从下到上)
【发布时间】:2017-02-20 10:58:26
【问题描述】:

当我将可拖动元素(克隆)拖到可放置容器中时,它们会从上到下堆叠,有没有办法将其反转?我尝试向父元素添加相对位置,并为可拖动元素添加“位置:绝对;底部:0”,但它不能正常工作,因为它们都保持在完全相同的位置,所以它们不会堆叠在一起。

代码和一个 JSFiddle 示例:

https://jsfiddle.net/f3gzrtar/

HTML:

  <div class="containtment">

   <div class="cube">
  </div>

   <div class ="makeMeDroppable">
   </div>

  </div> <!--containtment-->

CSS

.containtment {
  height: 600px;
  width: 600px;
  border: 1px solid;
 }

 .cube {
  width: 100px;
  height: 100px;
  background-color: blue;
  position: relative;
  display: block;
  margin: 1%;
 }

 .cube.ui-draggable-dragging {
   background: green;
  }

 .draggableHelper {
   width: 100px;
   height: 100px;
   background-color: blue;
   position: relative;
   display: block;
  }

 .makeMeDroppable {
  float: right;
  width: 200px;
  height: 300px;
  border: 1px solid #999;
  position: relative;
  overflow: hidden;
  }

JS

$(document).ready(function() {

$('.cube').draggable({
  containment: "parent",
  cursor: 'move',
  helper: myHelper,
 })

 function myHelper() {
   return '<div class="draggableHelper"></div>';
 }

 $('.makeMeDroppable').droppable({
   drop: handleDropEvent
 });

 function handleDropEvent(event, ui) {
   var draggable = ui.draggable;
 $(".makeMeDroppable").append("<div class='cube'></div>")
  }
 });

【问题讨论】:

标签: jquery jquery-ui jquery-ui-draggable


【解决方案1】:

基于此answer,您可以使用display: table-cellvertical-align: bottom 从底部堆叠div。请注意,在放置容器上使用 float: right 意味着这将不起作用。您需要一个额外的周围元素,例如this fiddle

如果您不关心某些较旧的浏览器,您也可以使用 flexbox:

.makeMeDroppable {
  display: flex;
  float: none;
  width: 200px;
  height: 300px;
  border: 1px solid #999;
  position: relative;
  overflow: hidden;
  justify-content: flex-end;
  flex-direction: column;
}

flex fiddle

【讨论】:

  • 有趣的是,我只是在看那个答案,但我从没想过 float:right 是它不起作用的罪魁祸首。它有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-29
相关资源
最近更新 更多