weixin_33725270 2012-03-04 13:34 采纳率: 0%
浏览 203

将jQuery Masonry与AJAX结合使用

I'm using the jQuery plugin "Masonry" (http://masonry.desandro.com/) to sort some DIVs. These DIVs are loaded with AJAX and somehow, the plugin starts too early and get the positions wrong. How can I set a proper delay in these functions?

function loadALLtheposts(id) {
$.ajax({
    url: "******.php",
    data: {userID: id},
    type: "POST",
    dataType: "text",
    contentType: "application/x-www-form-urlencoded; charset=utf-8",
    success: function (data) {
        $('#timeline-posts').html(data);
        masonry_index();
    }
});
}

function masonry_index() {
$('#timeline-posts').masonry({
    itemSelector : '.post-wrapper',
    columnWidth : 266
});
}

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • ℙℕℤℝ 2012-10-24 12:58
    关注

    You should use the standard DOM event - DOMSubtreeModified(deprecated)

    $("#timeline-posts").bind("DOMSubtreeModified", function() {
        masonry_index(); //this is called only after the #timeline-posts changes.
        $(this).unbind('DOMSubtreeModified'); //if required do this
    });
    

    Use MutationObservers instead.

    var target = $('#some-id');
    var observer = new MutationObserver(function(mutations, observer) {
        //mutation occured
        masonry_index();
    });
    var config = { attributes: true, childList: true, characterData: true }; //there are others that you can choose. Follow the link above.
    observer.observe(target, config);
    //if you want to stop observing
    observer.disconnect();
    

    Please note that non of the above code is tested. :). Other SO links which might be helpful - link, link

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料