Only apply jQuery waterfall 'reflow' after the images have loaded

隐身守侯 提交于 2019-12-05 19:20:51

Have a look at the imagesLoaded library / jquery plugin: http://imagesloaded.desandro.com/

Try this in your success function:

success: function(html) {

$("#buildcontainer").append(html);

var loaded = 0,
imgs = $("img"),
totalImgs = imgs.length;

imgs.load(function() {
    ++loaded;

    // Check if all images have loaded
    if (loaded === totalImgs) {
        // Run waterfall
        $('#buildcontainer').waterfall('reflow');
    }
});

}

Assuming all the images are being added in here:

success: function(html) {                   
  $("#buildcontainer").append(html).waterfall('reflow');
}

Then, you can do the following to monitor them all after adding them:

success: function(html) {

  function checkCnt() {
      if (remainingCnt === 0) {
          $("#buildcontainer").waterfall('reflow');
      }
  }

  var remainingCnt = $("#buildcontainer").append(html)
    .find("img")
    .filter(function() { return !this.complete; })
    .load(function() {
        // one more is loaded now, see if they are all loaded
        --remainingCnt;
        checkCnt();
     }).length;
  checkCnt();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!