3

Masonry jQuery プラグインを使用してアイテムのグリッドを揃えています。

ここに私が持っているものがあります:

結果

予想される行動:

予想される行動

どうすればこの効果を達成できますか? さまざまなオプションを試しましたが、何も機能しません。

各ボックスの HTML は次のとおりです。

<div class="sa-visual-grid-item" id="sa-visual-grid-item-<?=$id?>">
    <div class="sa-selected-box"></div>
    <input type="hidden" name="selected[<?=$id?>]" value="1" />
    <img class="sa-img" src="<?=$img_url?>" />
    <div class="sa-desc">
        <div class="sa-name"><?=$name?></div>
        <div class="sa-price"><?=$price?></div>
    </div>
</div>

そしてCSS:

.sa-visual-grid {
    height: auto;
    margin: 0 auto;
    text-align: center;
}
.sa-visual-grid-item {
    background: white;
    float: left;
    width: 250px;
    padding: 15px;
    margin: 10px 15px;
    border: 1px solid #bbb;
    box-shadow: 0px 5px 15px 0px #efefef;
    cursor:pointer;
    text-align: center;
}
.sa-selected-box {
    display: none;
    position:absolute;
    z-index:100;
    border: 8px solid #00aa00;
    width:254px;
    padding:15px;
    margin-top:-25px;
    margin-left:-25px;
}
4

3 に答える 3

2

できません。ビンを最も効率的にパックするアルゴリズムです。見栄えを良くするために設計されたものではありません。たぶん、wookmark プラグインを試すことができます。他にもたくさんあります。

于 2013-04-25T17:28:23.807 に答える
0

これが古いことは知っていますが、同じ問題に遭遇し、グーグルでこの投稿に出くわしました。ライブラリを少し変更することで、この問題を解決できました。Masonry.prototype._getItemLayoutPosition 関数を次のように変更します (//レンガの配置の下の変更を見逃さず、//setHeight を適用します)。

Masonry.prototype._getItemLayoutPosition = function( item, count ) {
    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 );
    // get the minimum Y value from the columns
    var minimumY = Math.min.apply( Math, colGroup );
    var shortColIndex;

    if (this._getOption('alignLTR') == true)
    {
        shortColIndex = count - ((Math.floor(count / colGroup.length) * colGroup.length));
    } 
    else {
        shortColIndex = colGroup.indexOf(minimumY);
    }

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

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

    return position;
  };

また、この関数を呼び出すループを変更して、アイテムをループするときに各アイテムのカウントを渡す必要があります。

proto._layoutItems = function( items, isInstant ) {
  this._emitCompleteOnItems( 'layout', items );

  if ( !items || !items.length ) {
    // no items, emit event with empty array
    return;
  }

  var queue = [];

  var itemCount = 0;
  items.forEach( function( item, itemCount ) {
    // get x/y object from method
    var position = this._getItemLayoutPosition( item, itemCount );
    itemCount++;
    // enqueue
    position.item = item;
    position.isInstant = isInstant || item.isLayoutInstant;
    queue.push( position );
  }, this );

  this._processLayoutQueue( queue );
};

最後に、グリッドを作成するときに、新しいオプション 'alignLTR' を true に設定する必要があります。

var $grid = $('#myGrid').masonry({
            isAnimated: true,
            itemSelector: '.my-item',
            alignLTR: true
        });
于 2016-06-10T13:46:35.797 に答える