亲宝软件园·资讯

展开

js图片延迟加载的实现方法及思路

人气:0
大概的实现方式是:
在页面的load没有触发之前,把所有的指定id的元素内的img放入到imgs中,将所有的图片的src值放入到一个新建的_src属性中,把src设置为指定的显示图片。
然后,在document.body的scroll事件触发时,循环计算imgs中的img元素位置是否正好在浏览器显示框范围内,如果是,则将img元素的_src属性的值赋给src,这样图片就能显示出来。
这里比较麻烦地方是,如何计算img的位置,获得元素的相对于页面的绝对位置。通常是用offsetLeft和offsetTop,但这两个属性是元素的offsetParent指向的元素的相对位置, 如果offsetParent所指的元素是设置了浮动的或使用绝对定位,那么offsetLeft来获得绝对位置就不正确呢。
在这里我是将元素的所有父级元素的offsetTop之和来获得文档绝对位置的。
复制代码 代码如下:

//取元素的页面绝对 X位置
        var getLeft = function(El){
            var left = 0;
            do{
                left += El.offsetLeft;
            }while((El = El.offsetParent).nodeName != 'BODY');
            return left;
        };
        //取元素的页面绝对 Y位置
        var getTop = function(El){
            var top = 0;
            do{
                top += El.offsetTop;
            }while((El = El.offsetParent).nodeName != 'BODY');
            return top;
        };

在设置窗口的scroll事件时,ie使用是document.documentElement,而其他的浏览器都使用document。
 接下来是要获得浏览器显示窗口现对于文档的位置,用了下面的代码来计算
复制代码 代码如下:

//读取滚动条的位置和浏览器窗口的显示大小
             var top = isGoo ? document.body.scrollTop : document.documentElement.scrollTop,
                 left = isGoo ? document.body.scrollLeft :document.documentElement.scrollLeft,
                 width = document.documentElement.clientWidth,
                 height = document.documentElement.clientHeight;

谷歌浏览器要通过body来获得scrollTop,而其他浏览器通过documentElement。
 最后迭代判断img的位置,并显示图片
复制代码 代码如下:

//对所有图片进行批量判断是否在浏览器显示区域内
            for(var i=0 ; i < imgs.length; i++){
                var _top = getTop(imgs[i]),_left = getLeft(imgs[i]);
                //判断图片是否在显示区域内
                if( _top >= top &&
                    _left >= left &&
                    _top <= top+height &&
                    _left <= left+width){
                    var _src = imgs[i].getAttribute('_src');
                    //如果图片已经显示,则取消赋值
                    if(imgs[i].src !== _src){
                        imgs[i].src = _src;
                    }
                }
            }

可以运行的代码
复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>js图片延迟加载</title>
</head>
<body>
<table id="out1" style="height:2450px;">
<tr>
<td><img src="//www.qb5200.com/small/2011_4_22/photo163115955184917.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_4_13/photo163175537548397.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_4_16/photo16314557927776.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_4_10/photo1631932180569094.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_4_1/photo1630540642223.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_4_2/photo1638657445827.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_3_29/photo16384188604.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_29/photo16384188604.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_4_13/photo16311444254618471.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_15/photo16318118126415.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16310513957332.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16310342238265.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_3_14/photo163103452142757.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16310310447460.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_3_14/photo163103347259334.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16310342238265.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_3_14/photo163102333593532.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16323618120479.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16323942409450.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_15/photo1630653584606.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_3_12/photo163233846816799.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_13/photo16313216736705.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="//www.qb5200.com/small/2011_3_1/photo16313391752186220.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_1/photo163765186092776.jpg" alt="" /></td>
</tr>
</table>
<table id="out2" style="width:4883px">
<tr>
<td><img src="//www.qb5200.com/small/2011_4_22/photo163115955184917.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_4_13/photo163175537548397.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_4_16/photo16314557927776.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_4_10/photo1631932180569094.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_4_1/photo1630540642223.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_4_2/photo1638657445827.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_3_29/photo16384188604.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_29/photo16384188604.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_4_13/photo16311444254618471.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_15/photo16318118126415.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16310513957332.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16310342238265.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo163103452142757.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16310310447460.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo163103347259334.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16310342238265.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo163102333593532.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16323618120479.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_3_14/photo16323942409450.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_15/photo1630653584606.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_3_12/photo163233846816799.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_13/photo16313216736705.jpg" alt="" /></td>
<td><img src="//www.qb5200.com/small/2011_3_1/photo16313391752186220.jpg" alt="" /> </td>
<td><img src="//www.qb5200.com/small/2011_3_1/photo163765186092776.jpg" alt="" /></td>
</tr>
</table>
</body>
</html>
  <script type="text/javascript">
        function delayload(option){
            //读取参数
            //图片未加载时显示的图片
            var src = option.src ? option.src : '',
            //指定那些id下的img元素使用延迟显示
                id = option.id ? option.id : [];
            //图片列表
            var imgs = [];
            //获得所有的图片元素
            for(var i=0 ; i < id.length ; i++){
                var idbox = document.getElementById(id[i]),_imgs;
                if(idbox && (_imgs = idbox.getElementsByTagName('img'))){
                    for(var t=0 ; t < _imgs.length ; t++){
                        imgs.push(_imgs[t]);
                    }
                }
            }
            //将所有的图片设置为指定的loading图片
            for(var i=0 ; i < imgs.length ; i++){
                //图片本来的图片路径放入_src中
                imgs[i].setAttribute('_src',imgs[i].src);
                imgs[i].src = src;
            }
            //取元素的页面绝对 X位置
            var getLeft = function(El){
                var left = 0;
                do{
                    left += El.offsetLeft;
                }while((El = El.offsetParent).nodeName != 'BODY');
                return left;
            };
            //取元素的页面绝对 Y位置
            var getTop = function(El){
                var top = 0;
                do{
                    top += El.offsetTop;
                }while((El = El.offsetParent).nodeName != 'BODY');
                return top;
            };
            //是否为ie,并读出ie版本
            var isIE = !!navigator.userAgent.match(/MSIE\b\s*([0-9]\.[0-9]);/img);
            isIE && (isIE = RegExp.$1);
            //是否为chrome
            var isGoo = !!navigator.userAgent.match(/AppleWebKit\b/img);
            //获得可以触发scroll事件的对象
            var box = isIE ? document.documentElement : document;
            //body元素的scroll事件
            var onscroll = box.onscroll = function(){
                //读取滚动条的位置和浏览器窗口的显示大小
                var top = isGoo ? document.body.scrollTop : document.documentElement.scrollTop,
                    left = isGoo ? document.body.scrollLeft :document.documentElement.scrollLeft,
                    width = document.documentElement.clientWidth,
                    height = document.documentElement.clientHeight;
                //对所有图片进行批量判断是否在浏览器显示区域内
                for(var i=0 ; i < imgs.length; i++){
                    var _top = getTop(imgs[i]),_left = getLeft(imgs[i]);
                    //判断图片是否在显示区域内
                    if( _top >= top &&
                        _left >= left &&
                        _top <= top+height &&
                        _left <= left+width){
                        var _src = imgs[i].getAttribute('_src');
                        //如果图片已经显示,则取消赋值
                        if(imgs[i].src !== _src){
                            imgs[i].src = _src;
                        }
                    }
                }
            };
            var load = new Image();
            load.src = src;
            load.onload = function(){
                onscroll();
            };
        }
        delayload({id:['out1','out2'],src:'//img.jbzj.com/file_images/article/201307/2013072210300234.jpg'});
      </script>

加载全部内容

相关教程
猜你喜欢
用户评论