可能重复:
如何在DIV中将DIV居中?
请看下面的图片:
如何在红色容器div内水平居中放置灰色方块?这都是由同位素制成的,因此请记住这一点。
提前致谢。
即使父(红色)div始终在中间对齐,灰色,较小的div也不会对齐。在顶部图像中,当它们在单个列中对齐时,该列应位于包装器(红色)div的正中间。
实际上,实现同位素居中非常简单(刚刚完成此操作的站点在移动触摸设备和台式机设备上看起来都很不错)。您只需在此块末尾的常规同位素代码之前添加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>
找到最简单的解决方案。在同位素内部使用“砌体”布局:
$container.isotope({
itemSelector: '.pbox',
layoutMode: 'masonry',
masonry: {
isFitWidth: true
}
});
本文地址:http://jquery.askforanswer.com/jquerytongweisujuzhongzhongfu.html
文章标签:centering , jquery , jquery-isotope
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
文章标签:centering , jquery , jquery-isotope
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!