javascript preloader/progress/percentage

后端 未结 2 1905
轮回少年
轮回少年 2021-02-04 15:41

I\'m having trouble finding any good information on how to make a javascript(or jquery) progress bar WITH text that tells you the percentage.

I don\'t want a plug in, I

2条回答
  •  攒了一身酷
    2021-02-04 15:55

    elements have an onload event that fires once the image has fully loaded. Therefore, in js you can keep track of the number of images that have loaded vs the number remaining using this event.

    Images also have corresponding onerror and onabort events that fire when the image fails to load or the download have been aborted (by the user pressing the 'x' button). You also need to keep track of them along with the onload event to keep track of image loading properly.


    Additional answer:

    A simple example in pure js:

    var img_to_load = [ '/img/1.jpg', '/img/2.jpg' ];
    var loaded_images = 0;
    
    for (var i=0; i

    The example above doesn't handle onerror or onabort for clarity but real world code should take care of them as well.

提交回复
热议问题