【发布时间】:2017-12-22 13:11:00
【问题描述】:
我在 StackOverflow 上查找了很多答案,但似乎没有任何帮助。我有以下代码,它似乎在没有初始化砌体的情况下堆叠得很好(对于当前测试中的图像),但它失败了。我需要砌体的原因是因为图像不会总是对齐。
HTML
<div class='gallery'>
<!-- Grid sizing element 33.33% -->
<div class="grid-sizer"></div>
<!-- Two 50% -->
<div class='masonry-item gallery-item image-half'>
<a data-fluidbox="true" href="http://placehold.it/450x250"><img class="img-responsive" src="http://placehold.it/450x250" alt="450x250" /></a>
</div>
<div class='masonry-item gallery-item image-half'>
<a data-fluidbox="true" href="http://placehold.it/450x250"><img class="img-responsive" src="http://placehold.it/450x250" alt="450x250" /></a>
</div>
<!-- One 100% -->
<div class='masonry-item gallery-item image-full'>
<a data-fluidbox="true" href="http://placehold.it/900x400"><img class="img-responsive" src="http://placehold.it/900x400" alt="900x400" /></a>
</div>
<!-- Three 33.33% -->
<div class='masonry-item gallery-item image-third'>
<a data-fluidbox="true" href="http://placehold.it/300x500"><img class="img-responsive" src="http://placehold.it/300x500" alt="300x500" /></a>
</div>
<div class='masonry-item gallery-item image-third'>
<a data-fluidbox="true" href="http://placehold.it/300x500"><img class="img-responsive" src="http://placehold.it/300x500" alt="300x500" /></a>
</div>
<div class='masonry-item gallery-item image-third'>
<a data-fluidbox="true" href="http://placehold.it/300x500"><img class="img-responsive" src="http://placehold.it/300x500" alt="300x500" /></a>
</div>
</div>
CSS
.gallery .gallery-item {
margin-bottom: 20px;
float: left;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
height: 400px;
}
.grid-sizer { width: 33.33%; }
.gallery .gallery-item img{
max-width: 100%;
}
.gallery .image-full {
width: 100%;
background: red;
}
.gallery .image-half {
width: 50%;
background: blue;
}
.gallery .image-third {
width: 33.33%;
background: green;
}
.grid-item {
margin-bottom: 10px;
}
JavaScript
var $gallery = $(".gallery");
$gallery.imagesLoaded(function() {
$gallery.masonry({
percentPosition: true,
columnWidth: '.grid-sizer',
itemSelector: '.gallery-item',
});
});
【问题讨论】:
-
this 有帮助吗?
-
谢谢,在那种情况下它有效,但是当我添加更多项目时,它又坏了
标签: javascript jquery css jquery-masonry