欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

原生javascript實(shí)現(xiàn)圖片滾動(dòng)、延時(shí)加載功能

 更新時(shí)間:2015年01月12日 10:13:43   投稿:hebedich  
這篇文章主要介紹了使用原生javascript實(shí)現(xiàn)圖片滾動(dòng)、延時(shí)加載功能,思路與方法均分享給大家,希望對(duì)大家能有所幫助。

實(shí)現(xiàn)效果:下拉滾動(dòng)條時(shí),圖片出現(xiàn)在可見區(qū)域時(shí),才開始加載

思路:

(1)img標(biāo)簽,把真實(shí)的圖片地址,放在自己設(shè)置的屬性里面,如 lazy-src

(2)獲取img離頁(yè)面的高度(在JQ里是offset().top),原生是:

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

(3)判斷img出現(xiàn)的位置是否在可見區(qū)域里:

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

復(fù)制代碼 代碼如下:

//保存document在變量里,減少對(duì)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;
                    // 判斷圖片是否在可見區(qū)域
                    if(justTop>scrollTop&&offsetTop<(scrollTop+windowHeight)){

                        this.isLoad(hSrc,n);
                    }
                }

            }

以下為詳細(xì)代碼:

復(fù)制代碼 代碼如下:

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在變量里,減少對(duì)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;
                    // 判斷圖片是否在可見區(qū)域
                    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;
            // 滾動(dòng):添加定時(shí)器,防止頻繁調(diào)用loadImg函數(shù)
            window.onscroll = function(){
                clearTimeout(timer);
                timer = setTimeout(function(){
                    _that.loadImg();
                },100);
            }
        }
    }

效果圖:

以上就是本文的全部?jī)?nèi)容了,實(shí)現(xiàn)的效果不比jQuery插件實(shí)現(xiàn)的差吧,代碼還簡(jiǎn)潔,小伙伴們參考下吧。

相關(guān)文章

最新評(píng)論