js 实现图片延迟加载
<!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" style1="height:2450px;">
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
</table>
<table id="out2" style1="width:4883px">
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126185813100585.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126182393278906.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126182393278906.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126182393278906.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126182393278906.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126182393278906.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126182393278906.jpg" alt="" width="400px" height="400px" /></td>
</tr>
<tr>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126182393278906.jpg" alt="" width="400px" height="400px" /> </td>
<td><img src="img/load.gif" _src="http://demo.kesion.com/UploadFiles/2012/admin/20126182393278906.jpg" alt="" width="400px" height="400px" /></td>
</tr>
</table>
</body>
</html>
<script type="text/javascript">
function delayload(option){
var _src ='';
//指定那些id下的img元素使用延迟显示
var 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]);
}
}
}
//取元素的页面绝对 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 / 2 ;
//对所有图片进行批量判断是否在浏览器显示区域内
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;
}
}
}
};
//加载滚动条
onscroll();
}
delayload({id:['out1','out2']});
</script>