Jquery Drag & Drop as Windows taskbar

I am an intermediate jquery experience so I need help here with awesome Demo interface:

Demo

But I don't know why it is not a drag and drop to another div with the same id "#drophere". please do it the same way as Windows Taskbar. Thanks to

$( function() {
    $( "#dragme" ).draggable({
      appendTo: 'body',
      containment: '#drophere'
    });
  } );
      

body {
  position: fixed;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
#dragme {
  position: absolute;
  background: rgba(0, 100, 200, 0.6);
  width: 100%;
  height: 50px;
  left: 0;
  bottom: 0;
  text-align: center;
  font-size: 36px;
  z-index: 999;
}
#drophere {
  position: absolute;
  background: rgba(200, 100, 0, 0.1);
  text-align: center;
}
.top {top:0;width: 100%;height: 50px;}
.left {left:0;width: 50px;height: 100%;}
.bottom {bottom:0;width: 100%;height: 50px;}
.right {right:0;width: 50px;height: 100%;}
      

<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
  <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="dragme">Drag me</div>
<div id="drophere" class="top">Drop it Here</div>
<div id="drophere" class="left" style="padding-top:200px;">Drop it Here & stand it like me</div>
<div id="drophere" class="bottom">Drop it Here</div>
<div id="drophere" class="right" style="padding-top:200px;">Drop it Here & stand it like me</div>
      

Run codeHide result


+3


source to share


1 answer


As Michael mentioned, the id must be unique, you will want to use classes instead.

Also, IMHO, I would use . sortable () for this anchor and fill behavior, rather than draggable()

, you can do it draggable()

with some customization, but the sort will work with minimal effort.

Here is a working example and jsfiddle



 $(function() {
   $(".drophere").sortable({
     connectWith: ".drophere"
   }).disableSelection();
 });
      

body {
  position: fixed;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

#dragme {
  list-style-type: none;
  position: absolute;
  background: rgba(0, 100, 200, 0.6);
  width: 100%;
  height: 50px;
  left: 0;
  bottom: 0;
  text-align: center;
  font-size: 36px;
  z-index: 999;
}

.left #dragme, .right #dragme{
  height: 100%;
}
.drophere {
  position: absolute;
  background: rgba(200, 100, 0, 0.1);
  text-align: center;
  margin:0;
}

.top {
  top: 0;
  width: 100%;
  height: 50px;
}

.left {
  left: 0;
  width: 50px;
  height: 100%;
}

.bottom {
  bottom: 0;
  width: 100%;
  height: 50px;
}

.right {
  right: 0;
  width: 50px;
  height: 100%;
}
      

<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>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<ul class="top drophere">
  <li id="dragme">Drag me</li>
</ul>
<ul class="left drophere" style="padding-top:200px;"></ul>
<ul class="bottom drophere"></ul>
<ul class="right drophere" style="padding-top:200px;"></ul>
      

Run codeHide result


+2


source







All Articles