Using pace.js on loading appended images

我与影子孤独终老i 提交于 2019-12-10 10:57:37

问题


I wanted to use pace.js to show a progress bar while the appended images are being loaded, they provided an API but I have no idea how it works.

$('#loadImg').click(function() {
  Pace.start();
  var $con = $('#content');
  $con.append('<img src="http://lorempixel.com/500/500/">').imagesLoaded(function() {
    console.log('done!');
    Pace.stop();
  });
});

I used it with desandro/imagesloaded to call Pace.stop() but I don't see any progress bars.

I made a demo plunk for your convenience.


回答1:


You first need to disable pace on page load using:

"startOnPageLoad" : false

Also quoting from pace documentation:

Elements being rendered to the screen is one way for us to decide that the page has been rendered.

So we can say that loading of 'image' should successfully complete the pace progress:

"elements": { 
    "selectors": ["#image"] // assign id="image" to img
}

Load the pace with these options provided in script tag:

data-pace-options='{ "elements": { "selectors": ["#image"] }, "startOnPageLoad": false }'

Now just call Pace.restart() every time click on link 'Load Image'.

No need to call Pace.stop(). (it automatically detects that #image is done loading)

Updated plunk



来源:https://stackoverflow.com/questions/20120029/using-pace-js-on-loading-appended-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!