如何将div从一个.panel-body div移动到另一个.panel-body div

时间:2016-10-30 07:10:40

标签: javascript jquery html jquery-ui jquery-ui-draggable

Jquery-UI draggable不允许从一个区域拖动到另一个区域。

下面的代码段包含带有字段的组标题,详细信息和组页脚区域。 归档只能在区域内拖动。尝试将字段从组头区域移动到其他区域是不可能的:如果移动到其区域之外,则可拖动的div变为不可见。

如何解决这个问题,以便div可以在具有panel-body类的div之间移动?

看起来像是

 $(".panel-body").droppable({
    accept: ".designer-field"
  });
jquery-ui忽略

命令。



$(function() {

  var startpos,
    selected = $([]),
    offset = {
      top: 0,
      left: 0
    };

/*  $(".designer-panel-body").droppable({
    accept: ".designer-field"
  });
*/
  $(".designer-field").draggable({

    stop: function(event, ui) {
      $(ui.draggable).detach().appendTo(this);
      },
  
  
    start: function(event, ui) {
      var $this = $(this);
      if ($this.hasClass("ui-selected")) {
        selected = $(".ui-selected").each(function() {
          var el = $(this);
          el.data("offset", el.offset());
        });
      } else {
        selected = $([]);
        $(".designer-field").removeClass("ui-selected");
      }
      offset = $this.offset();
    },

    drag: function(event, ui) {
      // drag all selected elements simultaneously
      var dt = ui.position.top - offset.top,
        dl = ui.position.left - offset.left;
      selected.not(this).each(function() {
        var $this = $(this);
        var elOffset = $this.data("offset");
        $this.css({
          top: elOffset.top + dt,
          left: elOffset.left + dl
        });
      });
    }
  });



  $(".panel-resizable").resizable({
    minWidth: "100%",
    maxWidth: "100%",
    minHeight: 1
  });
});

.panel-resizable {
  min-height: 1px;
 /* overflow: hidden; */
  margin: 0;
  padding: 0;
}
.designer-field {
  border: 1px solid lightgray;
  white-space: pre;
  overflow: hidden;
  position: absolute;
}

.designer-panel-body {
  min-height: 1px;
/*  overflow: hidden; */
  margin: 0;
  padding: 0;
}

.panel-footer {
  padding: 0;
}

.designer-panel, .panel-body {
  margin: 0;
  padding: 0;
}

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>

<body>
  <div class='panel designer-panel'>
    <div class='panel-body designer-panel-body panel-resizable' style='height:2cm'>

      <div class='designer-field' style='left:5px;top:6px;width:180px'>field 1 in group 1 header</div>

      <div class='designer-field' style='left:54px;top :36px;width:160px'>field 2 in group 1 header</div>
    </div>

    <div class='panel-footer'>Group 1 Header</div>
  </div>

  <div class='panel designer-panel'>
    <div class='panel-body designer-panel-body panel-resizable' style='height:1cm'>
      <div class='designer-field' style='left:24px;top:2px;width:140px'>field in detail group</div>
    </div>
    <div class='panel-footer panel-primary'>Detail <a role="button" id="designer-detail-addband"> Add group</a>
    </div>
  </div>

  <div class='panel'>
    <div class='panel-body designer-panel-body panel-resizable' style='height:1cm'>

      <div class='designer-field' style='left:44px;top:2px;width:140px'>field in group 1 footer</div>
    </div>
    <div class='panel-footer panel-warning'>Group 1 Footer</div>
  </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

这是因为您的设计师 - 面板 - 身体&#39;课程和&#39;面板可调整大小&#39; class有溢出设置为隐藏,导致拖动元素在面板外拖动后隐藏。

您需要取消设置,并且您应该能够将元素从一个面板拖动到另一个面板。

这是一个展示区域之间拖放元素的演示 http://codepen.io/jyloo/pen/GjbmLm

<强> HTML

<div class="drag-area">
  <div class="area">Droppable Area 1</div>
  <div class="box">Box1</div>
  <div class="box">Box2</div>
</div>
<div class="drag-area">
  <div class="area">Droppable Area 2</div>
</div>
<div class="result">-</div>

<强> CSS

.drag-area {
  width: 200px;
  height: 200px;
  background: #fff;
  display: inline-block;
  vertical-align: top;
  position: relative;
  margin-left: 30px;
  border: 1px solid #ddd;
  box-shadow: 3px 3px 6px 3px rgba(0,0,0,0.06)
}
.area {
  position:absolute;
  margin: 0 auto;
  color: #ccc;
  font-size: 20px;
  bottom: 10px;
  left: 20px;
}
.box {
    width: 50px;
    height: 50px;
    background: #673AB7;
    color: #fff;
    text-align: center;
    z-index: 2;
    border:2px solid #512DA8;

}
.result {
  display: inline-block;
  margin-left: 30px;
}

<强> JS

$( ".box" ).draggable({
  scope: 'demoBox',
  revertDuration: 100,
  start: function( event, ui ) {
    //Reset
    $( ".box" ).draggable( "option", "revert", true );
    $('.result').html('-');
  }
});

$( ".drag-area" ).droppable({
   scope: 'demoBox',
   drop: function( event, ui ) {
     var area = $(this).find(".area").html();
     var box = $(ui.draggable).html()     
     $( ".box" ).draggable( "option", "revert", false );

     //Display action in text
     $('.result').html("[Action] <b>" + box + "</b>" +
                       " dropped on " + 
                       "<b>" + area + "</b>")

     //Realign item
     $(ui.draggable).detach().css({top: 0,left: 0}).appendTo(this);
   }
})