<!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>
<tr>
<td><img src="https://www.jb51.net/small/2011_4_22/photo163115955184917.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_4_13/photo163175537548397.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_4_16/photo16314557927776.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_4_10/photo1631932180569094.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_4_1/photo1630540642223.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_4_2/photo1638657445827.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_3_29/photo16384188604.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_29/photo16384188604.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_4_13/photo16311444254618471.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_15/photo16318118126415.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16310513957332.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16310342238265.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_3_14/photo163103452142757.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16310310447460.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_3_14/photo163103347259334.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16310342238265.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_3_14/photo163102333593532.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16323618120479.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16323942409450.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_15/photo1630653584606.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_3_12/photo163233846816799.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_13/photo16313216736705.jpg" alt="" /></td>
</tr>
<tr>
<td><img src="https://www.jb51.net/small/2011_3_1/photo16313391752186220.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_1/photo163765186092776.jpg" alt="" /></td>
</tr>
</table>
<table>
<tr>
<td><img src="https://www.jb51.net/small/2011_4_22/photo163115955184917.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_4_13/photo163175537548397.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_4_16/photo16314557927776.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_4_10/photo1631932180569094.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_4_1/photo1630540642223.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_4_2/photo1638657445827.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_3_29/photo16384188604.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_29/photo16384188604.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_4_13/photo16311444254618471.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_15/photo16318118126415.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16310513957332.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16310342238265.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo163103452142757.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16310310447460.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo163103347259334.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16310342238265.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo163102333593532.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16323618120479.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_3_14/photo16323942409450.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_15/photo1630653584606.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_3_12/photo163233846816799.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/small/2011_3_13/photo16313216736705.jpg" alt="" /></td>
<td><img src="https://www.jb51.net/small/2011_3_1/photo16313391752186220.jpg" alt="" /> </td>
<td><img src="https://www.jb51.net/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>
您可能感兴趣的文章: