jQuery预加载显示百分比

加载的文件个数 /全部文件个数 的百分比就是进度

<pre>
    <img class="bj loadimg" loadimg="/weiqingshu/images/1/bj.jpg"/>
</pre>

<script type="text/javascript" src="/caigeci/js/jquery.imgpreload.min.js"></script>
<script type="text/javascript">
$(function(){
    imgNum=0;
    var load_img = [];
    /*$('.loadimg').each(function () {
        var loadimg=$(this).attr('loadimg');
        load_img.push(loadimg);
    })*/
    load_img.push( '/images/answer0.jpg' );
    load_img.push( '/images/answer1.jpg' );

    jQuery.imgpreload(load_img, {
        each: function () {
            /*this will be called after each image loaded*/
            var status = $(this).data('loaded') ? 'success' : 'error';
            if (status == "success") {
                var v = (parseFloat(++imgNum) / load_img.length).toFixed(2);
                console.log(Math.round(v * 100) + "<sup>%</sup>");
            }
        },
        all: function() {
            console.log('全部加载完成');
            /*$('.loadimg').each(function () {
            var loadimg=$(this).attr('loadimg');
            $(this).attr('src',loadimg);*/
    });
});
</script>

发表评论

© 2022 狸玖技术站 - 基于 WordPress