[原创]预加载的实现

简单版的预加载

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />

    <title>Page Title</title>


    <script src="../jquery.1.10.js"></script>
</head>
<style>
    .loading{
        position: fixed;top: 0;
        left:0;
        width: 100%;
        height: 100%;
        background-color: #eee;
        text-align: center;
        font-size: 30px;
    }
    .progress{
        margin-top: 300px;
    }
    .btn.hover{
        background-color: #eee;
    }

</style>
<body>
    <div class="box">
        <img src="http://preview.quanjing.com/top034/top-924268.jpg" alt="pic" id="img" height="500">
        <p>
            <a href="#" class="btn" data-control="prev">上一页</a>
            <a href="#" class="btn" data-control="next">下一页</a>
        </p>
    </div>

    <div class="loading">
        <p class="progress">0%</p>
    </div>

</body>
<script>
        var imgs = [
            "http://preview.quanjing.com/top034/top-924268.jpg",
            "http://preview.quanjing.com/CPS1/152-0111.jpg",
            "http://preview.quanjing.com/bld011/bld125797.jpg",
            "http://preview.quanjing.com/east025/east-ep-a41-8167880.jpg"
        ]
        var index = 0,
            len = imgs.length,
            count = 0,
            $progress = $('.progress');

        $('.btn').on('click',function (params) {
            if('prev' === $(this).data('control')){
                index = Math.max(0,--index);
            }else{
                index = Math.min(len -1, ++index)
            }
            document.title = (index + 1) + '/' +len;
            $('#img').attr('src',imgs[index]);
        })
        $.each(imgs, function (i, src) {

            var imgObj = new Image();
            $(imgObj).on('load error', function () {

                $progress.html(Math.round((count + 1) / len * 100) + "%");
                console.log("$progress  ", (count + 1) / len * 100);

                if (count >= len - 1) {
                    $('.loading').hide();
                    document.title = '1/' + len;
                }
                count++
            });
            imgObj.src = src;
        })
</script>
</html>

封装成jquery插件

//---------preload.js
(function($){
    function preload(imgs, options) {
        this.imgs = (typeof imgs === 'string') ? [imgs] : imgs;
        this.opts = $.extend({}, preload.defaults, options);
        this._unoredered() //加下划线说明方法是私有
    }
    preload.defaults = {
        each: null,//所有图片加载完成后执行
        all: null//所有图片加载完毕后执行
    };
    preload.prototype._unoredered = function(){  //无序加载

          var img = this.imgs,
              opts = this.opts,
              count = 0,
              len = imgs.length;

          $.each(imgs, function (i, src) {
              debugger;
              if(typeof src != 'string') return;
              var imgObj = new Image();
              $(imgObj).on('load error', function () {              
                  opts.each && opts.each(count);//判断 each方法是否存在
                   if (count >= len - 1) {
                      opts.all  && opts.all();
                   }
                   count++
               });
               imgObj.src = src;
        })
    }
    $.extend({
        preload:function(imgs,opts){
            new preload(imgs,opts)
        }
    })

})(jQuery)
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Page Title</title>
    <script src="../jquery.1.10.js"></script>
</head>
<style>
    .loading{
        position: fixed;top: 0;
        left:0;
        width: 100%;
        height: 100%;
        background-color: #eee;
        text-align: center;
        font-size: 30px;
    }
    .progress{
        margin-top: 300px;
    }
    .btn.hover{
        background-color: #eee;
    }

</style>
<body>
    <div class="box">
        <img src="http://preview.quanjing.com/top034/top-924268.jpg" alt="pic" id="img" height="500">
        <p>
            <a href="#" class="btn" data-control="prev">上一页</a>
            <a href="#" class="btn" data-control="next">下一页</a>
        </p>
    </div>

    <div class="loading">
        <p class="progress">0%</p>
    </div>

</body>
<script src="./preload.js"></script>
<script>
        var imgs = [
            "http://preview.quanjing.com/top034/top-924268.jpg",
            "http://preview.quanjing.com/CPS1/152-0111.jpg",
            "http://preview.quanjing.com/bld011/bld125797.jpg",
            "http://preview.quanjing.com/east025/east-ep-a41-8167880.jpg"
        ],
        $progress = $('.progress'),
        len = imgs.length;
        $.preload(imgs,{
            each:function(count){
                console.log(count);

                $progress.html(Math.round((count + 1) / len * 100) + "%");
            },
            all:function(){
                $('.loading').hide();
                document.title = '1/' + len;
            }
        })
        var index = 0;


        $('.btn').on('click',function (params) {
            if('prev' === $(this).data('control')){
                index = Math.max(0,--index);
            }else{
                index = Math.min(len -1, ++index)
            }
            document.title = (index + 1) + '/' +len;
            $('#img').attr('src',imgs[index]);
        })

</script>
</html>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值