码迷,mamicode.com
首页 > Web开发 > 详细

js实现页面图片加载进度条

时间:2015-02-26 18:11:45      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

//html
<div id="loading" class="loading">
        <div class="load">
            <span id="loadingSpan"></span>
        </div>
</div>
<div id="content"><img src="content_01.jpg" /><img src="content_02.jpg" /><img src=="content_03.jpg" /><img src="content_04.jpg" /></div>
//js
<script type="text/javascript">
       var core = {
            //资源预加载
            loadResources: function() {
                var bodyHeight = $(window).height();
                $("#loading").height(bodyHeight);
                var imgPath = "images/";
                var sourceArr = [
                 content_01.jpg,
                 content_02.jpg,
                 content_03.jpg,
                 content_04.jpg,
                 content_05.jpg
                ];
                for (var i = 0; i < sourceArr.length; i++) {
                    sourceArr[i] = (imgPath + sourceArr[i]);
                }
                var loadImage = function(path, callback) {
                    var img = new Image();
                    img.onload = function() {
                        img.onload = null;
                        callback(path);
                    }
                    img.src = path;
                }
                var imgLoader = function(imgs, callback) {
                    var len = imgs.length, i = 0;
                    while (imgs.length) {
                        loadImage(imgs.shift(), function(path) {
                            callback(path, ++i, len);
                        });
                    }
                }
                var bodyh = document.documentElement.clientHeight;


                imgLoader(sourceArr, function(path, curNum, total) {
                    var percent = curNum / total;
                    document.getElementById(loadingSpan).innerHTML = 资源加载中... + Math.floor(percent * 100) + "%";
                    if (percent == 1) {
                        setTimeout(core.showPage, 300);
                    }
                });
            } (),
            //资源加载完毕,显示页面内容
            showPage: function() {
                $("#loading").remove();
                $("#content").show();
            }
        }
    </script>

转自:tx lol

js实现页面图片加载进度条

标签:

原文地址:http://www.cnblogs.com/jasonzeng/p/4301614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!