【问题标题】:jQuery isotope centering [duplicate]jQuery同位素居中[重复]
【发布时间】:2012-08-21 08:25:52
【问题描述】:

可能重复:
How to center DIV in DIV?

请看下图:

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

提前致谢。

即使父(红色)div 始终居中对齐,灰色的、较小的 div 也不是。 在顶部图像中,当它们在单个列中对齐时,该列应位于包装器(红色)div 的正中间。

【问题讨论】:

  • 绝对不是。因为目前的效果是通过javascript(同位素)实现的
  • 根据包含的div计算位置
  • 好吧,无论如何它都不起作用,我也尝试在红色和较小的灰色之间放置一个包装 div,但仍然没有运气。
  • 请参阅下面答案中的居中解决方案。基本上,它使用 David DeSandro 网站上推荐的附加代码扩展了 Isotope。
  • 这不是“DIV 中的 DIV”的副本

标签: jquery centering jquery-isotope


【解决方案1】:

实际上,为 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>

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

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

【讨论】:

  • 另外,确保容器的margin-rightmargin-left 明确设置为auto。否则它不会工作,至少不是在 chrome 中。
  • 我很难将它与排水沟一起使用。排水沟不起作用。
  • 这有效,但仅当我缩小浏览器时。当我再次扩大规模时,什么也没有发生。没有其他人有同样的问题?
  • 将这些放在一起时遇到问题,答案是否已过时?如果没有,我们可以举一个小提琴的例子吗?
【解决方案2】:

找到最简单的解决方案。使用 Isotope 中的“砌体”布局:

$container.isotope({ 
  itemSelector: '.pbox', 
  layoutMode: 'masonry',
  masonry: { 
    isFitWidth: true 
  }
});

【讨论】:

  • 问题是同位素而不是砌体!
  • 也适用于同位素。
  • 值得注意的是,砌体是同位素的一部分。以这种方式在 Isotope 中使用它:$container.isotope({ itemSelector: '.pbox', masonry: { isFitWidth: true }, });
猜你喜欢
  • 2016-12-03
  • 2012-11-22
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
相关资源
最近更新 更多