0

这是一个类似问题的小提琴:http: //jsfiddle.net/DougCassidy/Gawe7/ 它工作得很好,但是,我想在盒子之间有一些很好的间距。如果右边距被视为盒子之间的最小间距会很好,这样如果间距低于该数量,它会将所有内容向下移动一行。此外,当前连续的最后一个框不应有右边距。

然后,一些底部边距。固定数量就可以了,或者等于当前水平间距的数量也可以。

$(new Array(9).join('<div class="invisible"></div>\n')).insertBefore('.stretch');

$(window).on('resize', function() {
$('#container').height($('.box').last().position().top +     $('.box').last().outerHeight());

//make it still justify when there's only one row
if ($('.box').first().position().top == $('.box').last().position().top) {
    $('.invisible').hide();
} else {
    $('.invisible').show();
}
}).resize();
4

1 回答 1

0

你不需要 JavaScript,只需要负边距:

http://codepen.io/cimmanon/pen/iHGCd

.gallery {
  margin: -5px;
  display: -ms-flexbox;
  display: -webkit-flex;
  -webkit-flex-wrap: wrap;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -ms-flex-pack: justify;
  -webkit-justify-content: space-between;
  justify-content: space-between;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
}
@supports (flex-wrap: wrap) {
  .gallery {
    display: flex;
  }
}

.gallery img {
  margin: 5px;
}

HTML:

<div class="gallery">
    <img src="http://placehold.it/200x100" />
    <!-- etc -->
</div>

因为边距不会在弹性项目上折叠,所以如果你像这样在所有边上添加边距效果最好。

于 2013-11-04T15:01:39.977 回答