Masonry giving container 0 height

こ雲淡風輕ζ 提交于 2020-01-05 13:12:13

问题


I'm working on some stuff that involves a responsive masonry layout. I have a container that has a max-width of 960px but scales down with the browser. My grid items have a percentage-based width (100%, 66.666666etc%, 33.33333etc%). For some reason, when I pass a function to the columnWidth option, it always gives the container a height of 0. This is how I'm instantiating Masonry.

$(window).load(function() {
  var $container;
  $container = $("#grid_container");
  $container.masonry({
    columnWidth: function(containerWidth) {
      return containerWidth / 3;
    },
    itemSelector: ".grid_item",
    isFitWidth: true,
    isResizable: true,
    gutter: 10
  });
});

Any idea why it'd be doing this? Since I'm using $(window).load it should be able to calculate the height fine. Am I missing something really obvious here? Is it simply not possible to use percentage column widths with Masonry?


回答1:


In Masonry, a function type isn't a permitted value for the columnWidth property.

Try instead to:

  1. Pass a CSS selector (e.g., .grid_box) as the columnWidth property.
  2. In CSS, add the selector to the appropriate percentage width of its parent container (e..g, .grid_box{ width: 20%; }).
  3. To your HTML, add an empty <div class="grid_box"></div> to the container.


来源:https://stackoverflow.com/questions/21394607/masonry-giving-container-0-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!