Search code examples
javascriptjqueryhtmlcsspackery

Jquery UI can't drag from left to right side


On my page I'm using the Jquery UI extension to sort divs. The divs are arranged in 2 columns. I created a JS Bin, please take a look on it.

When you drag all 3 divs to the left side, you can't get them on the right side again. Where is the mistake?

$(document).ready(function() {
  var $grid = $('.grid').packery({
    itemSelector: '.gridItem',
    gutter: '.gridGutter',
    percentPosition: true
  });

  var $items = $grid.find('.gridItem').draggable();
  $grid.packery('bindUIDraggableEvents', $items);
});
* {
  margin: 0px;
  padding: 0px;
  font-family: 'Francois One', sans-serif;
  font-size: 16px;
  text-decoration: none;
}
.grid {
  width: 1000px;
  background-color: aqua;
}
.gridItem {
  width: 49%;
  background-color: coral;
}
.gridGutter {
  width: 2%;
}
@media screen and (max-width: 1000px) {
  .grid {
    width: 100%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script src="https://unpkg.com/[email protected]/dist/packery.pkgd.min.js"></script>

<div class="grid">
  <div class="gridGutter"></div>
  <div class="gridItem">div 1</div>
  <div class="gridItem">div 2</div>
  <div class="gridItem">div 3</div>
</div>


Solution

  • The Packery documentation says the following:

    Without columnWidth set, dragged items can only be dropped in place of other items.

    You can set a selector for the columnWidth so it takes that element's width as the column's width.

    $(document).ready(function() {
      var $grid = $('.grid').packery({
        itemSelector: '.gridItem',
        gutter: '.gridGutter',
        percentPosition: true,
        columnWidth: '.gridSizer'
      });
    
      var $items = $grid.find('.gridItem').draggable();
      $grid.packery('bindUIDraggableEvents', $items);
    });
    * {
      margin: 0px;
      padding: 0px;
      font-family: 'Francois One', sans-serif;
      font-size: 16px;
      text-decoration: none;
    }
    .grid {
      width: 1000px;
      background-color: aqua;
    }
    .gridSizer,
    .gridItem {
      width: 49%;
    }
    .gridItem {
      background-color: coral;
    }
    .gridGutter {
      width: 2%;
    }
    @media screen and (max-width: 1000px) {
      .grid {
        width: 100%;
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
    <script src="https://unpkg.com/[email protected]/dist/packery.pkgd.min.js"></script>
    
    <div class="grid">
      <div class="gridSizer"></div>
      <div class="gridGutter"></div>
      <div class="gridItem">div 1</div>
      <div class="gridItem">div 2</div>
      <div class="gridItem">div 3</div>
    </div>