最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

原生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)在可見(jiàn)區(qū)域時(shí),才開(kāi)始加載

思路:

(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)的位置是否在可見(jiàn)區(qū)域里:

  .在瀏覽器的可見(jiàn)區(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;
                    // 判斷圖片是否在可見(jiàn)區(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;
                    // 判斷圖片是否在可見(jiàn)區(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)論

方正县| 德钦县| 连南| 陆川县| 定西市| 二连浩特市| 光泽县| 聊城市| 会泽县| 常宁市| 拜城县| 邯郸市| 临夏县| 水富县| 资源县| 天柱县| 广水市| 兴山县| 龙江县| 都兰县| 巴林右旗| 贺兰县| 安丘市| 和顺县| 阳东县| 安多县| 通化县| 东方市| 化德县| 南川市| 交口县| 三台县| 垣曲县| 黄龙县| 土默特左旗| 茶陵县| 麻栗坡县| 南丰县| 会东县| 辽中县| 闽侯县|