原生javascript實現圖片滾動、延時載入功能,javascript延時

來源:互聯網
上載者:User

原生javascript實現圖片滾動、延時載入功能,javascript延時

實現效果:下拉捲軸時,圖片出現在可見地區時,才開始載入

思路:

(1)img標籤,把真實的圖片地址,放在自己設定的屬性裡面,如 lazy-src

(2)擷取img離頁面的高度(在JQ裡是offset().top),原生是:

   img.getBoundingClientRect().top + document.body.scrollTop||document.documentElement.scrollTop

(3)判斷img出現的位置是否在可見地區裡:

  .在瀏覽器的可見地區,justTop>scrollTop&&offsetTop<(scrollTop+windowHeight),這裡的justTop是圖片的offsetTop+圖片高度

複製代碼 代碼如下:
//儲存document在變數裡,減少對document的查詢
            var doc = document;
            for(var n=0,i = this.oImg.length;n<i;n++){
                //擷取圖片預留位置圖片地址
                var hSrc = this.oImg[n].getAttribute(this.sHolder_src);
                if(hSrc){
                    var scrollTop = doc.body.scrollTop||doc.documentElement.scrollTop,
                        windowHeight = doc.documentElement.clientHeight,
                        offsetTop = this.oImg[n].getBoundingClientRect().top + scrollTop,
                        imgHeight = this.oImg[n].clientHeight,
                        justTop = offsetTop + imgHeight;
                    // 判斷圖片是否在可見地區
                    if(justTop>scrollTop&&offsetTop<(scrollTop+windowHeight)){

                        this.isLoad(hSrc,n);
                    }
                }

            }

以下為詳細代碼:

複製代碼 代碼如下:
function LGY_imgScrollLoad(option){
        this.oImg = document.getElementById(option.wrapID).getElementsByTagName('img');
        this.sHolder_src = option.holder_src;
        this.int();
    }
    LGY_imgScrollLoad.prototype = {
        loadImg:function(){
            //儲存document在變數裡,減少對document的查詢
            var doc = document;
            for(var n=0,i = this.oImg.length;n<i;n++){
                //擷取圖片預留位置圖片地址
                var hSrc = this.oImg[n].getAttribute(this.sHolder_src);
                if(hSrc){
                    var scrollTop = doc.body.scrollTop||doc.documentElement.scrollTop,
                        windowHeight = doc.documentElement.clientHeight,
                        offsetTop = this.oImg[n].getBoundingClientRect().top + scrollTop,
                        imgHeight = this.oImg[n].clientHeight,
                        justTop = offsetTop + imgHeight;
                    // 判斷圖片是否在可見地區
                    if(justTop>scrollTop&&offsetTop<(scrollTop+windowHeight)){
                        //alert(offsetTop);
                        this.isLoad(hSrc,n);
                    }
                }

            }
        },
        isLoad:function(src,n){
            var src = src,
                n = n,
                o_img = new Image(),
                _that = this;
            o_img.onload = (function(n){
                _that.oImg[n].setAttribute('src',src);
                _that.oImg[n].removeAttribute(_that.sHolder_src);
            })(n);
            o_img.src = src;

        },
        int:function(){
            this.loadImg();
            var _that = this,
                timer = null;
            // 滾動:添加定時器,防止頻繁調用loadImg函數
            window.onscroll = function(){
                clearTimeout(timer);
                timer = setTimeout(function(){
                    _that.loadImg();
                },100);
            }
        }
    }


以上就是本文的全部內容了,實現的效果不比jQuery外掛程式實現的差吧,代碼還簡潔,小夥伴們參考下吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.