14

似乎当我尝试加载页面时,所有图像都堆叠在一起。但是,如果您要单击将您带到同一页面的链接(例如主页链接),那么砌体就会启动。所以我认为砌体加载得太早了,就像在 jquery 准备好页面之前一样。

这是我的jQuery调用:

$(document).ready(function(){
    $('#image_roll_container').masonry({
        itemSelector: '.box'
    });

....

这是有问题的页面:

http://ratattoos.com/

它在 Firefox 和 IE8 中运行良好。

4

12 回答 12

20

我已经设法通过以下调整解决了这个问题:

<script type="text/javascript">
    $(document).ready(function(){
        $('img').load(function(){
            $(".content_photo").masonry();
        });
        $(".content_photo").masonry();
    });
</script>
于 2012-02-10T21:34:45.893 回答
11

看起来我需要一个名为 imagesLoaded 的插件,以便 Monsry 脚本与 chrome 和 safari 等工具正常工作

于 2011-10-01T16:37:07.820 回答
8

尝试了此线程中建议的所有内容,没有任何效果,然后发现:

$(window).load(function(){   $('#content').masonry(); });

现在工作正常,在这里找到它: https ://github.com/desandro/masonry/issues/35

原帖作者: https ://github.com/desandro

于 2013-04-22T19:50:35.833 回答
5

您对 imagesLoaded 的看法是正确的。它在 Firefox 中运行良好,但在 Chrome/Safari 中堆叠。

这是链接https://masonry.desandro.com/layout.html#imagesloaded

代码:

var $container = $('#container');

$container.imagesLoaded( function(){
  $container.masonry({
    itemSelector : '.box'
  });
});
于 2012-03-26T14:07:51.597 回答
1

我最近遇到了这个问题。为了解决这个问题,我使用了 img 的宽度和高度属性。问题自己解决了。

于 2012-10-04T19:33:45.507 回答
0

另一种方法,如果你知道图像高度,是在加载 Masonry 之前在 CSS 中分配它们,那么布局比等待图像更快。例如,如果您的所有图像大小相同,则此方法有效。然后,您的网站仍会在慢速连接(例如移动设备)上快速加载。

我在这里发布了一些替代方法的脚本:http:
//instancia.net/loading-jquery-masonry-on-mobile/

如果您使用此脚本,请编辑数字以匹配您的。

于 2013-01-26T20:04:28.707 回答
0

在 Firefox 和我的 iPad 2 上,砌体工作正常,但在 OS X 上的 chrome 和 safari 中,元素在页面加载时重叠/堆叠,直到窗口调整大小甚至发生。在挖掘 jquery.masonry.js 的代码后,我发现我可以在创建砌体后立即触发 resize() ,以便所有元素正确重新排列。现在一切正常。

jQuery(document).ready(function(){
var $container = $('#container');
$container.imagesLoaded(function(){
    $container.masonry({
    itemsSelector: '.thumbnail',
    isFitWidth: true
    }).resize();
}); 
})

所有其他解决方案:(window).load、在 CSS 和 img 属性中设置宽度和高度等,对我来说都不起作用。

于 2013-03-24T23:04:06.710 回答
0

就像詹妮弗说的那样,它需要这些浏览器中的高度才能正确显示。我使用 php 的 getimagesize() 函数来获取图像的高度和宽度。现在完美运行。

于 2013-10-22T02:29:28.773 回答
0
<script>
        var container = document.querySelector('#masonry');
        var msnry = new Masonry( container, {
            itemSelector: '.item',
            "columnWidth": 200,
        });
        $('.item img').load(function(){
                var msnry = new Masonry( container, {
                itemSelector: '.item',
                "columnWidth": 200,
            });
        })
</script>
于 2013-11-13T08:41:36.700 回答
0

如果使用 $('img').load(function() F5(refesh) => 错误

新方法:

$(window).on('load resize', function() {
  if ($('.masonry-wrap').length) {
    $('.masonry-wrap')
    .css('max-width', $(window).width());
  }
});
$(window).on('load', function() {
  if ($('.masonry-wrap').length) {
    setTimeout(function() {
      $('.masonry').masonry({
        columnWidth: 0,
        itemSelector: '.grid-item'
      });
    }, 1);
  }
});
<div class="masonry-wrap">
  <div class="masonry">
    <div class="grid-item">...</div>
    <div class="grid-item">...</div>
    ....
  </div>
</div>

于 2015-01-31T19:27:17.767 回答
0

“加载”事件将为 DOM 中的每个图像触发,这太过分了。当 DOM 中的最后一个图像加载时,您需要更新砌体的布局。这是代码:

$(document).ready(function(){
    // init the masonry on document ready event;
    // at this point the images are not loaded so the layout will break UNLESS you set up the correct values for the "width" and "height" attributes of the img tags
    $('.content_photo').masonry();

    // to make sure the layout will not break apart we update the masonry layout just after the last image from the DOM was loaded
    var total_images = $('img').length;
    var counter = 1;
    $('img').load(function() {
        if(counter == total_images) {
            alert('the last image in the DOM was loaded. now we can update the masonry layout');
            $('.content_photo').masonry('layout');
        }
        counter++;
    });
});
于 2016-04-07T11:13:59.217 回答
0

如前所述,我遇到了相反的问题:第一次加载在 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

因为我没有时间跟踪可能受此错误影响的每个浏览器版本,所以我为所有浏览器切换到后一种方法。

于 2016-08-22T17:14:32.497 回答