jQuery Masonry and Ajax Append Items?

前端 未结 13 1399
借酒劲吻你
借酒劲吻你 2020-12-08 07:19

I am trying to use some ajax and the jQuery Masonry plugin to add some items - but for some reason the new items aren\'t getting the masonry applied ?

I\'m using

13条回答
  •  隐瞒了意图╮
    2020-12-08 07:41

    For Masonry v3.2.2 (latest at the time of this post), this is what works:

    Assuming newHtml is a string like this:

  • item 1
  • item 2
  • item 3
  • You process it like this:

    $.get(apiUrl, function(newHtml) {
        var textArr = newHtml.split("");
        var elArr = [];
        $.each(textArr, function(i,v) {
            if (v) {
                elArr.push($(v)[0]);
            }
        });
        $(this).append(elArr);
        $container.waitForImages( function() {
            $container.masonry('appended', elArr);
        });
    }
    

    Took me 2 hours to find out this!

提交回复
热议问题