可拖动元素堆叠到可放置容器中(从下到上)

Draggable elements to stack into droppable container(bottom to top)

当我将可拖动元素(克隆)拖入可放置容器时,它们从上到下堆叠,有没有办法反转它?我试过向父元素添加一个相对位置,并向 dragabble 添加一个 "position:absolute;bottom: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>")
  }
 });

基于此answer you can use display: table-cell and vertical-align: bottom to stack divs from the bottom. Note that using float: right on the drop container means this won't work however. You need an additional surrounding element like in 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