jQuery Masonry排序顺序从左到右而不是从上到下?

时间:2013-07-08 20:33:20

标签: jquery jquery-masonry

在目录网站上使用Masonry。默认情况下,显示的项目按日期排序,然后按部件号排序。我的图像高度不一。问题是,如果我错了,请纠正我,Masonry将第2行,第1项设置在第1行中的最短项目下方,而不是在页面左侧。换句话说,当我的数据库返回某个订单时,Masonry将这些项目从上到下排列(当变化高度时),而不是从左到右。

我在文档中找不到任何可以控制它的内容。有没有办法强迫砌体按顺序从左到右依次保持物品,只是根据高度垂直漂浮?

我试图发布插图,但显然我没有资格这样做。这是图示的链接:

enter image description here

5 个答案:

答案 0 :(得分:3)

Masonry中没有选项可以对项目进行排序,因为它是一个简单的插件,可以逐个放置砖块。选择新的砖块位置很简单:尽可能放置它。

但是在这种情况下,可以做的是通过添加一条定义排序的两行来更改脚本,假设所有砖块的宽度相同(例如,总是5行)。

为了证明这一点,我使用了jQuery Masonry(已压缩),您可以在this Fiddle中查看。

<强> JS

$.Mason.prototype._placeBrick = function(e) {
    var n = $(e),
        r, i, s, o, u;
    r = Math.ceil(n.outerWidth(!0) / this.columnWidth), r = Math.min(r, this.cols);
    if (r === 1) s = this.colYs;
    else {
        i = this.cols + 1 - r, s = [];
        for (u = 0; u < i; u++) o = this.colYs.slice(u, u + r), s[u] = Math.max.apply(Math, o)
    }
    var a = Math.min.apply(Math, s),
        f = 0;
    for (var l = 0, c = s.length; l < c; l++)
        if (s[l] === a) {
            f = l;
            break
        }
    /* Add new calculation, what column next brick is in: */
    f = $(e).index() % this.cols; /* Get col index f: Just divide with element's index */
    a = s[f]; /* This is current height for f-th column */
    /* END of customizing */
    var h = {
        top: a + this.offset.y
    };
    h[this.horizontalDirection] = this.columnWidth * f + this.offset.x, this.styleQueue.push({
        $el: n,
        style: h
    });
    var p = a + n.outerHeight(!0),
        d = this.cols + 1 - c;
    for (l = 0; l < d; l++) this.colYs[f + l] = p;
};

答案 1 :(得分:3)

受比利的回答(以及skobaljic的回答:)的启发我刚刚更改了shortColIndex和minimumY的初始化以实现此行为。可能会更容易一点 - 使用colGroup的大小 - 具有不同列数的响应式布局仍然有用。

以下是v3.3.2的完整代码:

Masonry.prototype._getItemLayoutPosition = function( item ) {
  item.getSize();
  // how many columns does this brick span
  var remainder = item.size.outerWidth % this.columnWidth;
  var mathMethod = remainder && remainder < 1 ? 'round' : 'ceil';
  // round if off by 1 pixel, otherwise use ceil
  var colSpan = Math[ mathMethod ]( item.size.outerWidth / this.columnWidth );
  colSpan = Math.min( colSpan, this.cols );

  var colGroup = this._getColGroup( colSpan );
  // ### HACK: sort by natural order, not by min col height
  // get the minimum Y value from the columns
  // var minimumY = Math.min.apply( Math, colGroup );
  // var shortColIndex = utils.indexOf( colGroup, minimumY );
  var shortColIndex = jQuery(item.element).index() % colGroup.length;
  var minimumY = colGroup[shortColIndex];

  // position the brick
  var position = {
    x: this.columnWidth * shortColIndex,
    y: minimumY
  };

  // apply setHeight to necessary columns
  var setHeight = minimumY + item.size.outerHeight;
  var setSpan = this.cols + 1 - colGroup.length;
  for ( var i = 0; i < setSpan; i++ ) {
    this.colYs[ shortColIndex + i ] = setHeight;
  }

  return position;
};

答案 2 :(得分:0)

简单的答案是&#34; no&#34;

Masonry 几乎按照定义重新排列适合的东西并假设顺序并不重要。我说&#34;几乎&#34;因为我认为你是对的,所以在文档中的任何地方都没有说明 - 但这就是它的工作原理。

答案 3 :(得分:0)

看看Masonry Ordered。我没有对此进行测试,但看起来它可能是您正在寻找的解决方案:http://masonry-ordered.tasuki.org/options/

答案 4 :(得分:0)

skobaljic's answer启发(仅与Masonry v2兼容),我已经攻击v3做同样的事情。这是:

Masonry.prototype._getItemLayoutPosition = function( item ) { // Hack Masonry to order items by their order in the DOM
    item.getSize();
    // how many columns does this brick span
    var remainder = item.size.outerWidth % this.columnWidth;
    var mathMethod = remainder && remainder < 1 ? 'round' : 'ceil';
    // round if off by 1 pixel, otherwise use ceil
    var colSpan = Math[ mathMethod ]( item.size.outerWidth / this.columnWidth );
    colSpan = Math.min( colSpan, this.cols );

    var col = $(item.element).index() % 3; // HACK : determine which column we want based on the element's index in the DOM

    var colGroup = this._getColGroup( colSpan );
    colGroup = [this.colYs[ col ]]; // HACK : return only the column we want
    // get the minimum Y value from the columns
    var minimumY = Math.min.apply( Math, colGroup );
    var shortColIndex = col; // HACK

    // position the brick
    var position = {
      x: this.columnWidth * shortColIndex,
      y: minimumY
    };

    // apply setHeight to necessary columns
    var setHeight = minimumY + item.size.outerHeight;
    this.colYs[ shortColIndex ] = setHeight; // HACK : set height only on the column we used

    return position;
};

希望这有助于某人

相关问题