带进度条的图片预加载效果

61 篇文章 0 订阅
53 篇文章 0 订阅

带进度条的图片预加载效果

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2014年7月10日 17:17:23 星期四

 

原理:使用两张图片实现:一张小的缩略图,一张大的高清图,将缩略图拉大到高清图的尺寸(这就是模糊的效果),然后加载高清图片,加载完成修改<img>标签的src。

 

  图片加载效果:

<script type="text/javascript">
        (function () {
            var timing = function () {
                this._timer = null;
                this._percent = 0;
                this._isLoading = false;
                this._isPause = false;
                this._step = 2;
            };

            timing.prototype = {
                start: function (url, obj) {
                    var self = this;
                    self._percent = 0;

                    self._loadImg(url);

                    self._timer = setInterval(function () {
                        self._judge(url, obj);
                        self._scroll();
                    }, 30);
                },
                _pause: function () {
                    this._isPause = true;
                    this._step = 0;
                },
                _restart: function () {
                    this._isPause = false;
                    this._step = 3;
                },
                _scroll: function () {
                    $('.loading-progress').width(this._percent + "%");
                    this._percent += this._step;
                },
                _loadImg: function (url) {
                    var self = this;
                    var img = new Image();
                    $(img).load(function () {
                        setTimeout(function () {//模拟加载 
                            self._isLoading = true;
                        }, 2000);
                    });
                    img.src = url;
                },
                _judge: function (url, obj) {
                    if (this._percent >= 70 && !this._isLoading) {
                        this._pause();
                    }
                    if (this._percent >= 70 && this._isLoading && this._isPause) {
                        this._restart();
                    }
                    if (this._percent >= 100) {
                        obj.attr("src", url);
                        this.destroy();
                    }
                },
                destroy: function () {
                    this._percent = 0;
                    clearInterval(this._timer);
                    $('.loading').hide();
                }
            }

            if (typeof window.timing == 'undefined' || window.timing == null) {
                window.timing = new timing();
            }
        })();

    </script>

 

替换图片代码:

$(function () {
            timing.start('../img/load-image.jpg', $("img").eq(0));
        });

 

Html代码:

<div><img src="../img/load-image-s.jpg" style="width: 409px; height: 307px;" /></div>

 

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀 2014年7月10日 17:17:23 星期四

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值