Packery horizontal scrolling layout issue

768 views Asked by At

I am trying to write a horizontally scrolling, draggable/sortable list using packery.

Basically I want to make a horizontal version of this codepen: http://codepen.io/desandro/pen/cbhDG

I have tried to modify it, and can get a horizontal list with scrollbar going no problem, the trouble is that as soon as i apply packery it wants to wrap the cells down vertically and it seems to remove the overflow, i cant find a way to control the size of the packery container

here is my attempt so far: http://codepen.io/anon/pen/dPYXJG

haml:

%div.all-slides
  %div.slides
    - (1..12).each do |i|
      %div.slide 

%div#cloned-slides.cloned-slides 

css:

html, body {
  height: 100%;
  width:100%;
  overflow: hidden;
  background: #8D939D;
  margin: 0;
}

.all-slides {

  width: 100%;
  height: 120px;
  white-space: nowrap;
  overflow-y: hidden;
  overflow-x: scroll;
  -webkit-overflow-scrolling: touch;
  padding: 1rem;
  background-color: #ccc;
}

.slide {
  display:inline-block;
  width: 100px;
  height: 60px;
  background: green;
  position: relative;
  margin:3px;
  z-index: 5;
  user-select: none;
}

javascript:

docReady( function() {

  var slidesElem = document.querySelector('.slides');
  var slideSize = getSize( document.querySelector('.slide') );
  var pckry = new Packery( slidesElem, {
        columnWidth : slideSize.outerWidth,
                rowHeight: slideSize.outerHeight

  });
  // get item elements
  var itemElems = pckry.getItemElements();
  // for each item...
  for ( var i=0, len = itemElems.length; i < len; i++ ) {
    var elem = itemElems[i];
    // make element draggable with Draggabilly
    var draggie = new Draggabilly( elem, {
      axis: 'x'
    });
    // bind Draggabilly events to Packery
    pckry.bindDraggabillyEvents( draggie );
  }

  // re-sort DOM after item is positioned
  pckry.on( 'dragItemPositioned', function( _pckry, draggedItem ) {
    var index = pckry.items.indexOf( draggedItem );
    var nextItem = pckry.items[ index + 1 ];
    if ( nextItem ) {
      slidesElem.insertBefore( draggedItem.element, nextItem.element );
    } else {
      slidesElem.appendChild( draggedItem.element );
    }

  });

});

any help would be greatly appreciated. cheers.

1

There are 1 answers

1
desandro On BEST ANSWER

Sure thing! http://codepen.io/desandro/pen/ByoQwN

I changed Packery option rowHeight: slldeSize.outerWidth to columnWidth: slideSize.outerHeight, and Draggabilly option axis: 'y' to axis:'x'. Everything else is handled with CSS.

var slidesElem = document.querySelector('.slides');
var slideSize = getSize( document.querySelector('.slide') );
var pckry = new Packery( slidesElem, {
  rowHeight: slideSize.outerHeight
});
// get item elements
var itemElems = pckry.getItemElements();
// for each item...
for ( var i=0, len = itemElems.length; i < len; i++ ) {
  var elem = itemElems[i];
  // make element draggable with Draggabilly
  var draggie = new Draggabilly( elem, {
    axis: 'y'
  });
  // bind Draggabilly events to Packery
  pckry.bindDraggabillyEvents( draggie );
}