砌体留下了几个缺口

Masonry is leaving several gaps

我已经检查了所有关于这个主题的帖子,但是 none 的解决方案对我有用。

这是我的网格的样子:

这是 jsfiddle,这是代码。 HTML:

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Masonry -->
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>
<link rel="stylesheet" href="css/style.css">


<!-- imagesLoaded -->
<script 
src="https://unpkg.com/imagesloaded@4.1/imagesloaded.pkgd.min.js">

// init Masonry
var $grid = $('.grid').masonry({
  itemSelector: '.grid-item',
  percentPosition: true,
  columnWidth: '.grid-sizer'
});
// layout Masonry after each image loads
$grid.imagesLoaded().progress( function() {
  $grid.masonry('layout');
});
</script>


<!-- Bootstrap -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">


<body>

  <div class="container">

        <div class="grid">

          <div class="grid-item">
              <img src="http://hiphopgoldenage.com/wp-content/uploads/2015/10/MellowManAce.jpg" alt="Mellow Man Ace">
          </div>

          <div class="grid-item">
              <img src="https://pbs.twimg.com/media/C2O6p6ZWgAA67jA.jpg" alt="...">
          </div>

          <div class="grid-item">
              <img src="http://images1.mtv.com/uri/mgid:file:docroot:mtv.com:/shared/promoimages/bands/c/cypres_hill/photos/cr_rene_cervantes/flipbook/CH-0141.jpg?enlarge=false&matte=true&matteColor=black&quality=0.85" alt="...">
          </div>

          <div class="grid-item">
              <img src="https://cdn-images-1.medium.com/max/600/1*VfCrfkNhTVwAxftgfebcog.jpeg" alt="...">
          </div>

          <div class="grid-item">
              <img src="https://images.genius.com/ace91b03cd16b6f3678660cd184af26a.500x323x1.jpg" alt="...">
          </div>

          <div class="grid-item">
              <img src="https://images.pexels.com/photos/271560/pexels-photo-271560.jpeg" alt="...">
          </div>

          <div class="grid-item">
              <img src="https://images.pexels.com/photos/147504/pexels-photo-147504.jpeg" alt="...">
          </div>

          <div class="grid-item">
              <img src="https://images.pexels.com/photos/99551/hot-air-balloon-valley-sky-99551.jpeg" alt="...">
          </div>

        </div>

CSS:

* { box-sizing: border-box; }

/* force scrollbar */
html { overflow-y: scroll; }
/* body { font-family: sans-serif; } */

/* ---- grid ---- */
.grid {
  background: #DDD;
}

/* clear fix */
.grid:after {
  content: '';
  display: block;
  clear: both;
}

/* ---- .grid-item ---- */
.grid-sizer,
.grid-item {
  width: 33.333%;
  float: left;
}
.grid-item img {
  display: block;
  max-width: 100%;
}

我在拧干什么?请帮我。 非常感谢!

您需要将 javascript 放在它自己的 script 标签中(没有 src)并且您需要将您使用的 jquery 与 imagesLoaded()$(document).ready()(或 $(function(){}))中并且无需在布局中指定 columnWidth,因为列宽在 CSS.

.grid {
  background: #DDD;
}

.grid:after {
  content: '';
  display: block;
  clear: both;
}

.grid-sizer,
.grid-item {
  width: 33.333%;
  float: left;
}

.grid-item img {
  display: block;
  max-width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>
<script src="https://unpkg.com/imagesloaded@4.1/imagesloaded.pkgd.min.js"></script>
<script>
  $(function() {
    var $grid = $('.grid').masonry({
      itemSelector: '.grid-item',
      percentPosition: true,
    });
    $grid.imagesLoaded().progress(function() {
      $grid.masonry('layout');
    });
  });
</script>

<div class="grid">
  <div class="grid-item">
    <img src="http://hiphopgoldenage.com/wp-content/uploads/2015/10/MellowManAce.jpg" alt="Mellow Man Ace">
  </div>
  <div class="grid-item">
    <img src="https://pbs.twimg.com/media/C2O6p6ZWgAA67jA.jpg" alt="...">
  </div>
  <div class="grid-item">
    <img src="http://images1.mtv.com/uri/mgid:file:docroot:mtv.com:/shared/promoimages/bands/c/cypres_hill/photos/cr_rene_cervantes/flipbook/CH-0141.jpg?enlarge=false&matte=true&matteColor=black&quality=0.85" alt="...">
  </div>
  <div class="grid-item">
    <img src="https://cdn-images-1.medium.com/max/600/1*VfCrfkNhTVwAxftgfebcog.jpeg" alt="...">
  </div>
  <div class="grid-item">
    <img src="https://images.genius.com/ace91b03cd16b6f3678660cd184af26a.500x323x1.jpg" alt="...">
  </div>
  <div class="grid-item">
    <img src="https://images.pexels.com/photos/271560/pexels-photo-271560.jpeg" alt="...">
  </div>
  <div class="grid-item">
    <img src="https://images.pexels.com/photos/147504/pexels-photo-147504.jpeg" alt="...">
  </div>
  <div class="grid-item">
    <img src="https://images.pexels.com/photos/99551/hot-air-balloon-valley-sky-99551.jpeg" alt="...">
  </div>
</div>

正确的做法是使用真实的宽度值,所以如果您想自动获取它(因为您的是百分比),只需获取网格项目的宽度即可。

这是一种方法:

$('.grid').masonry({
  // options
  itemSelector: '.grid-item',
  columnWidth: $(".grid-item").width()
});

示例运行:https://jsfiddle.net/hqLj5t55/