Packery水平滚动布局问题

时间:2014-12-01 11:28:47

标签: javascript horizontal-scrolling packery

我正在尝试使用packery编写一个水平滚动,可拖动/可排序的列表。

基本上我想制作这个codepen的横向版本: http://codepen.io/desandro/pen/cbhDG

我试图修改它,并且可以得到一个滚动条没有问题的水平列表,麻烦的是,只要我应用打包它想要垂直包裹单元格,它似乎删除溢出,我不能找到一种方法来控制包装容器的大小

这是我到目前为止的尝试: 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 );
    }

  });

});

任何帮助将不胜感激。 欢呼声。

1 个答案:

答案 0 :(得分:1)

当然可以! http://codepen.io/desandro/pen/ByoQwN

我将Packery选项rowHeight:slldeSize.outerWidth更改为columnWidth:slideSize.outerHeight,以及Draggabilly选项轴:&#39; y&#39;到轴:&#39; x&#39;。其他所有内容都用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 );
}