jQuery Masonry and Ajax Append Items?

前端 未结 13 1394
借酒劲吻你
借酒劲吻你 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:46

    Had a similar problem and instead used the following line (converted for your code). Sorry, I don't recall where I found it.

    In your code replace this:

    jQuery("#content").append(el).masonry( 'appended', el, true );
    

    With this:

    jQuery("#content").append(el).masonry( 'reload' );
    

    http://masonry.desandro.com/methods.html

    0 讨论(0)
  • 2020-12-08 07:51

    I added the following code after the append command and everything was fine:

    $grid.imagesLoaded().progress( function() {
        $grid.masonry('layout');
    });
    

    The reason:

    Unloaded images can throw off Masonry layouts and cause item elements to overlap. imagesLoaded resolves this issue. imagesLoaded is a separate script you can download at imagesloaded.desandro.com.

    source

    0 讨论(0)
  • 2020-12-08 07:52

    Just for future people who find this issue and the above solutions don't work for them: I found an issue with my selector and the element I added not having the same case, i.e. itemSelector was .Card but I was appending <div class="card">.

    Hope this helps.

    0 讨论(0)
  • 2020-12-08 07:55
    success: function (response) {
      if(response.length > 0) {
         var el = js(response); 
         setTimeout(function () {
           js("#masonry").append(el).masonry( 'appended', el).masonry('layout');
         }, 500);
      }
    }   
    

    works fine for me.

    0 讨论(0)
  • 2020-12-08 08:00

    This solution works for me:-

      jQuery.ajax({
        type: "POST",
        url: ajax_url,
        data: ajax_data,
        dataType: 'json',
        cache: false,
        success: function(response) {
          if (response.length > 0) {
            var $container = $('#container');
            var msnry = $container.data('masonry');
            var elems = [];
            var fragment = document.createDocumentFragment();
            for (var x in response) {
              var elem = $(response[x]).get(0);
              fragment.appendChild(elem);
              elems.push(elem);
            }
            $container.appendChild(fragment);
            msnry.appended(elems);
          }
        }
      });
    
    0 讨论(0)
  • 2020-12-08 08:02

    You are missing Masonry layout call. According to the docs you need to refresh the layout, executing .masonry() after every change (for instance .masonry('appended')):

    $grid.masonry()
      .append(elem)
      .masonry('appended', elem)
      // layout
      .masonry();
    

    (source: http://masonry.desandro.com/methods.html)

    0 讨论(0)
提交回复
热议问题