js 圖片懶載入

來源:互聯網
上載者:User

標籤:

圖片懶載入(圖片出現在可視地區再載入)

相容性:相容目前流行的全部瀏覽器,包括:相容到IE6)使用方法:
  • 引入相應的js檔案
    <script src="js/lazy.js"></script>

  • 初始化

Lazy.init(1000);可以傳入延時時間。預設是500ms

注意:

<img src="img/load.gif" data-lazy="img/2.jpg">

src 屬性放預先載入的圖片,data-lazy放實際展示的圖片

githut:https://github.com/chenjinxinlove/lazy

   /**     * 相容ie678不支援bind()辦法     * */    if (!function() {}.bind) {        Function.prototype.bind = function(context) {            var self = this                    , args = Array.prototype.slice.call(arguments);            return function() {                return self.apply(context, args.slice(1));            }        };    }    (function () {        var Lazy = function () {        };        var _imgs = [],        _num = 0;        /**         * 得到元素相當於視窗的高度         * @param _e 出入元素         * @returns {_number} 返回高度         */        Lazy.prototype._getEleViewHeight = function (_e) {            if(_e){                var _top = _e.offsetTop,                        _c = _e.offsetParent;                while (_c !== null){                    _top += _c.offsetTop;                    _c = _c.offsetParent;                }                if( typeof window.pageYOffset !== ‘undefined‘){                    return _top - window.pageYOffset;                }                else if(typeof  document.compatMode !== ‘undefined‘ && document.compatMode !== "BackCopt"){                    return _top - document.documentElement.scrollTop;                }                else{                    return _top - document.body.scrollTop;                }            }        };        /**         * 得到瀏覽內視窗高度         * @returns {number}         * @private         */        Lazy.prototype._getViewHeight = function () {            var _viewHeight = 0;            if( typeof window.innerHeight !== ‘undefined‘ ){                _viewHeight = window.innerHeight;            }            else if(typeof document.compatMode !== ‘undefined‘ && document.compatMode !== "BackCompt"){                _viewHeight = document.documentElement.clientHeight;            }            else{                _viewHeight = document.body.clientHeight;            }            return _viewHeight;        };        Lazy.prototype._getImg = function () {            var _i = document.getElementsByTagName("img");            for(var i = 0, len = _i.length; i < len; i++){                if(typeof  (_i[i].getAttribute("data-lazy"))){                    _imgs.push(_i[i]);                    _num++;                }            }        };        /**         * 匯入圖片         * @private         */        Lazy.prototype._loadImg = function () {            if(!_num)return;            var _viewHeihgt = this._getViewHeight();            for(var i = 0, len = _imgs.length; i < len; i++){                var _imgEle = _imgs[i];                if(this._getEleViewHeight(_imgEle) < _viewHeihgt){                    _imgEle.src =_imgEle.getAttribute("data-lazy");                    delete  _imgs[i];                    _num--;                }            }        };        /**         * 初始化,可以設定延時         */        Lazy.prototype.init = function (time) {            this._getImg();            var _time = time || 500;            window.onscroll = window.onload = function () {                setTimeout(function () {                    this._loadImg();                }.bind(this),_time);            }.bind(this);        }        window.Lazy = new Lazy();    })();

  

js 圖片懶載入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.