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

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配