如前所述,我遇到了相反的问题:第一次加载在 Mac OS X Safari 中运行良好,但是用所有新项目更改网格导致它们全部堆叠在左上角。此外,等待就绪事件并在我们的元素上设置 CSS 高度和宽度并没有解决它。
在我们的网站上,我们有显示在砌体网格中的数据类别,一次只显示一个类别。用户可以随时切换类别并触发 AJAX 请求以加载新数据。在最新的 Safari(9.1、10)和 Chrome 等浏览器中,我们可以在更改类别以换入所有新元素时简单地执行此操作:
// domData is HTML string from the server
// IMJS is our global variable that we use for globals and lookups
$("#divTemplateCategoryName").after(domData); // insert new HTML
var elementsToAdd = $(".grid-item-template-info"); //select the elements
IMJS.MasonryGrid.masonry('addItems', elementsToAdd); // tell masonry to add them
IMJS.MasonryGrid.masonry('layout'); // tell masonry to layout again
但是,在某些版本的 Safari 中不起作用,我们不得不这样做:
// domData is HTML string from the server
// IMJS is our global variable that we use for globals and lookups
IMJS.MasonryGrid.masonry('destroy'); // destroy the grid
$("#divTemplateCategoryName").after(domData); // insert new HTML
InitMasonry(); // re-do our entire masonry init
因为我没有时间跟踪可能受此错误影响的每个浏览器版本,所以我为所有浏览器切换到后一种方法。