jQuery同位素居中

21 jquery centering jquery-isotope

可能重复:
如何在DIV中居DIV?

请看下面的图片:

在此输入图像描述

如何使灰色方块在红色容器div内水平居中?这都是用同位素制成的,所以请记住这一点.

提前致谢.

在此输入图像描述 在此输入图像描述

即使父(红色)div总是在中间对齐,灰色,较小的div也不是.在顶部图像中,当它们在一列中对齐时,该列应位于包装器(红色)div的正中间.

Sys*_*get 21

实际上,为Isotope实现居中非常简单(刚刚完成了一个在移动触摸设备和桌面设备上看起来很好的网站).您只需在此块结尾处的常用Isotope代码之前包含David DeSandro存储库中的这段代码

<!-- centered layout extension http://isotope.metafizzy.co/ --> 

<script type="text/javascript">
$.Isotope.prototype._getCenteredMasonryColumns = function() {

    this.width = this.element.width();

    var parentWidth = this.element.parent().width();

    var colW = this.options.masonry && this.options.masonry.columnWidth || // i.e. options.masonry && options.masonry.columnWidth

    this.$filteredAtoms.outerWidth(true) || // or use the size of the first item

    parentWidth; // if there's no items, use size of container

    var cols = Math.floor(parentWidth / colW);

    cols = Math.max(cols, 1);

    this.masonry.cols = cols; // i.e. this.masonry.cols = ....
    this.masonry.columnWidth = colW; // i.e. this.masonry.columnWidth = ...
};

$.Isotope.prototype._masonryReset = function() {

    this.masonry = {}; // layout-specific props
    this._getCenteredMasonryColumns(); // FIXME shouldn't have to call this again

    var i = this.masonry.cols;

    this.masonry.colYs = [];
        while (i--) {
        this.masonry.colYs.push(0);
    }
};

$.Isotope.prototype._masonryResizeChanged = function() {

    var prevColCount = this.masonry.cols;

    this._getCenteredMasonryColumns(); // get updated colCount
    return (this.masonry.cols !== prevColCount);
};

$.Isotope.prototype._masonryGetContainerSize = function() {

    var unusedCols = 0,

    i = this.masonry.cols;
        while (--i) { // count unused columns
        if (this.masonry.colYs[i] !== 0) {
            break;
        }
        unusedCols++;
    }

    return {
        height: Math.max.apply(Math, this.masonry.colYs),
        width: (this.masonry.cols - unusedCols) * this.masonry.columnWidth // fit container to columns that have been used;
    };
};
</script>
Run Code Online (Sandbox Code Playgroud)

然后你就像往常一样设置同位素

<script type="text/javascript">
$(function() {
    var $container = $('#container');
    // the usual stuff for layouting, sorting, filtering, limiting clicks to zones...
</script>
Run Code Online (Sandbox Code Playgroud)

  • 另外,确保容器的`margin-right`和`margin-left`明确设置为`auto`.否则它不会工作,至少不是铬. (5认同)

小智 12

最简单的解决方案.使用同位素内的"砌体"布局:

$container.isotope({ 
  itemSelector: '.pbox', 
  layoutMode: 'masonry',
  masonry: { 
    isFitWidth: true 
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,砌体是同位素的一部分.在Isotope中以这种方式使用它:`$ container.isotope({itemSelector:'.pbox',masonry:{isFitWidth:true},});` (5认同)
  • 问题是同位素不是砖石! (2认同)
  • 也适用于同位素。 (2认同)