简体   繁体   English

ajax点击后,JQuery砌体不起作用

[英]JQuery masonry doesn't work after ajax click

I have a problem with my ajax code. 我的ajax代码有问题。 I created a DEMO from jsfiddle.net 我从jsfiddle.net创建了一个DEMO

In this demo you can see the grid system it is working fine. 在这个演示中,您可以看到它正常工作的网格系统。 But when you click the button for load more post the problem will be come on that time. 但是当你点击按钮加载更多帖子时,问题就会出现在那个时候。

What is the problem in my code? 我的代码有什么问题? Anyone can help me in this regard ? 谁能在这方面帮助我?

$(document).ready(function() {
function loadMore() {
    $('.posts-holder').append('<div class="kesif-gonderi-alani" style="height:300px; background:blue;">6</div>');
    $('.posts-holder').append('<div class="kesif-gonderi-alani" style="height:300px;">7</div>');
    $('.posts-holder').append('<div class="kesif-gonderi-alani" style="height:300px;">8</div>');
     $('.posts-holder').append('<div class="kesif-gonderi-alani" style="height:300px;">9</div>');
     $('.posts-holder').append('<div class="kesif-gonderi-alani" style="height:300px;">10</div>');

  }
  function ajax() {
    // fake request
    setTimeout(function(){
      loadMore();
      test();
    },1500);
  }
  $('.button').bind('click', function(e){
    e.preventDefault();
    ajax();

  });
  function test(){
  var $container = $(".posts-holder");
  $container.imagesLoaded(function() {
    $container.masonry({
      columnWidth: ".kesif-gonderi-alani",
      itemSelector: ".kesif-gonderi-alani"
    });
  });
  }
  test();
});

Use the Masonry append method insted of jQuery one. 使用jQuery one的砌体 append方法。 I've uploaded in your fiddle the function loadMore . 我已经在你的小提琴中上传了函数loadMore

function loadMore() {

  var $items = $('<div class="kesif-gonderi-alani" style="height:300px;">6</div><div class="kesif-gonderi-alani" style="height:300px;">7</div><div class="kesif-gonderi-alani" style="height:300px;">8</div><div class="kesif-gonderi-alani" style="height:300px;">9</div><div class="kesif-gonderi-alani" style="height:300px;">10</div>');

      $('.posts-holder').append($items)
      // add and lay out newly appended items
      .masonry('appended', $items);
}

The append method of Masonry need a jQuery object. Masonry的append方法需要一个jQuery对象。 So in your real ajax call you will create these new object and put them as argument in the append method. 因此,在真正的ajax调用中,您将创建这些新对象并将它们作为参数放在append方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM