砌体留下了几个空白

时间:2017-04-05 19:11:07

标签: javascript html css grid-layout masonry

我已经检查了有关此主题的所有帖子,但没有一个解决方案适合我。

这就是我的网格状态:

enter image description here

这是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%;
}

我在做什么?请帮我。 非常感谢提前!

2 个答案:

答案 0 :(得分:2)

您需要将javascript放在其自己的script标记中(不包含src),并且您需要将imagesLoaded()中使用的jquery放在$(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>

答案 1 :(得分:0)

正确的方法是使用实​​际的宽度值,所以如果你想自动获取(因为你的百分比),只需获得网格项宽度。

这是一种方法:

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

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