Packery:启用可拖动性和装订线

Packery: Enable Dragability And Gutter

我正在使用 Packery 一个 JavaScript 库和 jQuery 插件,可以制作由 David DeSandro 创建的无缝和可拖动布局。 我用它来设置我的投资组合,但下面的代码不起作用。我想让元素可拖动 (draggable) and adds horizontal and vertical space between item elements (gutter)。

<!DOCTYPE html>
<html>

<head>
    <title></title>
    <script type="text/javascript" src="https://unpkg.com/packery@2/dist/packery.pkgd.min.js"></script>    
    <script type="text/javascript" src="https://npmcdn.com/draggabilly@2/dist/draggabilly.pkgd.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

<body>

<div class="grid">
  <div class="grid-item grid-item--width2"><img src="http://www.naturalnews.com/gallery/640/Food/Fancy-Food.jpg" style="width: 100%; height: 100%;"></div>
  <div class="grid-item"><img src="https://s3-us-west-1.amazonaws.com/cdn.myawesomeurls.com/2317223231481790331/collections/9169011391481887406/bg_image.jpg" style="width: 100%; height: 100%;"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2 grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
</div>

</body>

</html>
<style type="text/css">
* { box-sizing: border-box; } 

body { font-family: sans-serif; }

.grid {
  background: #DDD;
  max-width: 1200px;
}

.grid:after {
  content: '';
  display: block;
  clear: both;
}

.grid-item {
  float: left;
  width: 100px;
  height: 100px;
  background: #C09;
  border: 2px solid hsla(0, 0%, 0%, 0.5);
}

.grid-item--width2 { width: 200px; }
.grid-item--height2 { height: 200px; }

.grid-item:hover {
  border-color: hsla(0, 0%, 100%, 0.5);
  cursor: move;
}

.grid-item.is-dragging,
.grid-item.is-positioning-post-drag {
  background: #C90;
  z-index: 2;
}

.packery-drop-placeholder {
  outline: 3px dashed hsla(0, 0%, 0%, 0.5);
  outline-offset: -6px;
  -webkit-transition: -webkit-transform 0.2s;
          transition: transform 0.2s;
}

</style>
<script type="text/javascript">

var $grid = $('.grid').packery({
  itemSelector: '.grid-item',
  columnWidth: 100, 
  gutter: 10
});

// make all grid-items draggable
$grid.find('.grid-item').each( function( i, gridItem ) {
  var draggie = new Draggabilly( gridItem ); // bind drag events to Packery

  $grid.packery( 'bindDraggabillyEvents', draggie );
});

$grid.on( 'click', '.grid-item', function( event ) {
  // change size of item by toggling large class
  $(  event.currentTarget  ).toggleClass('grid-item--large');
  // trigger layout after item size changes
  $grid.packery('layout');
});

</script>

始终在顶部包含 jquery.min.js。主要错误是由于该文件在末尾加载导致 未定义 packery 的未捕获类型错误 。其余所有代码都是正确的。

Here is the correct working solution.

var $grid = $('.grid').packery({
  itemSelector: '.grid-item',
  columnWidth: 100,
  gutter: 10
});

// make all grid-items draggable
$grid.find('.grid-item').each(function(i, gridItem) {
  var draggie = new Draggabilly(gridItem); // bind drag events to Packery

  $grid.packery('bindDraggabillyEvents', draggie);
});

$grid.on('click', '.grid-item', function(event) {
  // change size of item by toggling large class
  $(event.currentTarget).toggleClass('grid-item--large');
  // trigger layout after item size changes
  $grid.packery('layout');
});
* {
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
}

.grid {
  background: #DDD;
  max-width: 1200px;
}

.grid:after {
  content: '';
  display: block;
  clear: both;
}

.grid-item {
  float: left;
  width: 100px;
  height: 100px;
  background: #C09;
  border: 2px solid hsla(0, 0%, 0%, 0.5);
}

.grid-item--width2 {
  width: 200px;
}

.grid-item--height2 {
  height: 200px;
}

.grid-item:hover {
  border-color: hsla(0, 0%, 100%, 0.5);
  cursor: move;
}

.grid-item.is-dragging,
.grid-item.is-positioning-post-drag {
  background: #C90;
  z-index: 2;
}

.packery-drop-placeholder {
  outline: 3px dashed hsla(0, 0%, 0%, 0.5);
  outline-offset: -6px;
  -webkit-transition: -webkit-transform 0.2s;
  transition: transform 0.2s;
}
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/packery@2/dist/packery.pkgd.min.js"></script>
<script type="text/javascript" src="https://npmcdn.com/draggabilly@2/dist/draggabilly.pkgd.js"></script>



<div class="grid">
  <div class="grid-item grid-item--width2"><img src="http://www.naturalnews.com/gallery/640/Food/Fancy-Food.jpg" style="width: 100%; height: 100%;"></div>
  <div class="grid-item"><img src="https://s3-us-west-1.amazonaws.com/cdn.myawesomeurls.com/2317223231481790331/collections/9169011391481887406/bg_image.jpg" style="width: 100%; height: 100%;"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2 grid-item--height2"></div>
  <div class="grid-item"></div>
  <div class="grid-item grid-item--width2"></div>
  <div class="grid-item grid-item--height2"></div>
  <div class="grid-item"></div>
</div>