来源-脚本之家

浏览大型网站,特别是图片比较多的图片,如大型的电商网站,你会发现处了第一屏外,往下滚动的时候图片才加载出来,没必要一开始加载就要把全部图片加载出来,这样子打开网面的速度得到了很好提高。以下是笔者目前所想到的思路,如果有更好的思路,望不吝赐教。

其实实时加载图片思路很简单,页面一开始加载的时候把不需要一开始就加载的图片(如第二屏以下的图片,反正是要往下滚动才能看到的,不如把它们设置成往下滚动的时候再实时加载)路径保持到一个自定义的属性里,如:<img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023900736.jpg" alt="19岁女孩直面××× 7年前曾截肢" />,src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif"是一个1px*1px的透明gif图片,自定义属性data-src里的"http://jb.wanpin123.com/wpupload/2015/0427/20150427023900736.jpg"才是图片的路径,往下滚动到它的时候把data-src里的值替换掉src的值,然后把data-src属性移除掉……

如下是demo

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js图片实时加载</title>
<style>
*{padding:0; margin:0;}
.wp{width:1000px; margin:5px auto; padding:10px 0; border-bottom:1px solid #dedede; overflow:hidden;}
ul li{ display:block; width:250px; float:left; margin:5px 0; list-style:none;}
.aimg{ display:block; height:300px; max-width:348px; border:1px solid #EEE; background:url(http://ctc.qzs.qq.com/aoi/img/icenter/loading.gif) no-repeat center center;}
</style>
</head>
<body>
<div class="wp">
<a href="http://www.cnblogs.com/xiaomou2014"><img src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023900609.png" alt="xiaomou2014的头像"></a>
<a href="http://www.cnblogs.com/xiaomou2014">http://www.cnblogs.com/xiaomou2014</a>
cont2开始的图片是实时加载的
</div>
<div class="cont1 wp">
<h2>cont1</h2>
<ul>
<li><a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023900489.jpg" alt="19岁女孩直面××× 7年前曾截肢" /></a></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023901903.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023901237.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023902800.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023902983.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023903869.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023903654.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023903378.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023903225.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023904566.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023904242.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023904433.jpg" /></li>
</ul>
</div>
<div class="cont2 wp">
<h2>cont2</h2>
<ul>
<li><a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023900736.jpg" alt="19岁女孩直面××× 7年前曾截肢" /></a></li>
<li><a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023904478.jpg" alt="19岁女孩直面××× 7年前曾截肢" /></a></li>
<li><a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023904467.jpg" alt="19岁女孩直面××× 7年前曾截肢" /></a></li>
<li><a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023905599.jpg" alt="19岁女孩直面××× 7年前曾截肢" /></a></li>
<li><a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023933329.jpg" alt="19岁女孩直面××× 7年前曾截肢" /></a></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023933170.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023933115.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023933377.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023933402.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023934880.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023934178.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023934297.jpg" /></li>
</ul>
</div>
<div class="cont3 wp">
<h2>cont3</h2>
<ul>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023934214.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023934761.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023934938.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023934860.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023935550.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023935342.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023935596.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023935160.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023935731.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023935500.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023935297.jpg" /></li>
<li><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023936532.jpg" /></li>
</ul>
</div>

<script>
//兼容IE对getElementsByClassName
if (navigator.appName == "Microsoft Internet Explorer") {
document.getElementsByClassName = function() { 
var tTagName = "*"; 
if (arguments.length > 1) { 
tTagName = arguments[1]; 
} 
if (arguments.length > 2) { 
var pObj = arguments[2] 
} else { 
var pObj = document; 
} 
var objArr = pObj.getElementsByTagName(tTagName); 
var tRObj = new Array(); 
for ( var i = 0; i < objArr.length; i++) { 
if (objArr[i].className == arguments[0]) { 
tRObj.push(objArr[i]); 
} 
} 
return tRObj; 
} 
} 
//图片加载
function loadImg(imgClass,imgSrc){
//data-src:存放需要加载的图片的路径
var arrImg=document.getElementsByClassName(imgClass); //需要实时加载class为imgClass的图片数组
window.onscroll=function(){
for(var i=0; i<arrImg.length; i++){
if(!arrImg[i].getAttribute(imgSrc)) continue; 
if(document.documentElement.scrollTop+document.body.scrollTop+document.documentElement.clientHeight >arrImg[i].offsetTop){
arrImg[i].setAttribute("src",arrImg[i].getAttribute(imgSrc));
arrImg[i].removeAttribute(imgSrc);
}
}
}
}
loadImg("aimg","data-src");
</script>
</body>
</html>
如上demo里的cont1里的图片是一加载页面就显示的,对于首页布局是固定的,可以确认那些图片是第二屏以下的,但是对于内容页,主体内容容器里的图片出现的位置是不可控制的,这就需要加载完毕真个页面的时候再判断是不是在在可视范围内,如果是在可视范围内就把它加载出来(当然,首页也可这么做,但是如果网速很慢,那么打开首页的时候要等到整个页面架构加载完毕之后才显示图片,而不是从上而下加载出来),图片要设置width与height属性,这让它在页面占一个位置,不然为加载前它只占透明gif图片的大小位置。

如下demo针对内容页做了一点修改,window.onload、window.onscroll的时候都触发加载

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js图片实时加载</title>
<style>
*{padding:0; margin:0;}
.wp{width:1000px; margin:5px auto; padding:10px 0; border-bottom:1px solid #dedede; overflow:hidden;}
.fl{width:700px; padding:25px; float:left; border:1px solid #dedede; line-height:40px; overflow:hidden;}
.fr{width:220px; height:800px; float:right; background-color:#dedede;}
.aimg{ display:block; max-width:700px; margin:0 auto; background:url(http://ctc.qzs.qq.com/aoi/img/icenter/loading.gif) no-repeat center center;}
</style>
</head>
<body>
<div class="wp">
<a href="http://www.cnblogs.com/xiaomou2014"><img src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023900609.png" alt="xiaomou2014的头像"></a>
<a href="http://www.cnblogs.com/xiaomou2014">http://www.cnblogs.com/xiaomou2014</a>
</div>
<div class="wp">
<div class="fl">
<h2>7年前截肢的19岁×××晚期女孩</h2>
<p>http://www.cnblogs.com/xiaomou2014</p>
<img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023935500.jpg" width="310" height="428" />
<p>http://www.cnblogs.com/xiaomou2014</p>
<img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023935297.jpg" width="310" height="419" />
<p>http://www.cnblogs.com/xiaomou2014</p>
<img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023936532.jpg" width="310" height="428" />
<p>7年前截肢的19岁×××晚期女孩</p>
<a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023900736.jpg" alt="19岁女孩直面××× 7年前曾截肢" width="640" height="480" /></a>
<p>7年前截肢的19岁×××晚期女孩</p>
<a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023904478.jpg" alt="19岁女孩直面××× 7年前曾截肢" width="640" height="872" /></a>
<p>7年前截肢的19岁×××晚期女孩</p>
<a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023904467.jpg" alt="19岁女孩直面××× 7年前曾截肢" /></a>
<p>7年前截肢的19岁×××晚期女孩</p>
<a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023905599.jpg" alt="19岁女孩直面××× 7年前曾截肢" /></a>
<p>7年前截肢的19岁×××晚期女孩</p>
<a href="http://news.ifeng.com/a/20140909/41906442_0.shtml#p=1" title="7年前截肢的19岁×××晚期女孩" target="_blank" ><img class="aimg" src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023800403.gif" data-src="http://jb.wanpin123.com/wpupload/2015/0427/20150427023933329.jpg" alt="19岁女孩直面××× 7年前曾截肢" /></a>
</div>
<div class="fr">
</div>
</div>

<script>
//兼容IE对getElementsByClassName
if (navigator.appName == "Microsoft Internet Explorer") {
document.getElementsByClassName = function() { 
var tTagName = "*"; 
if (arguments.length > 1) { 
tTagName = arguments[1]; 
} 
if (arguments.length > 2) { 
var pObj = arguments[2] 
} else { 
var pObj = document; 
} 
var objArr = pObj.getElementsByTagName(tTagName); 
var tRObj = new Array(); 
for ( var i = 0; i < objArr.length; i++) { 
if (objArr[i].className == arguments[0]) { 
tRObj.push(objArr[i]); 
} 
} 
return tRObj; 
} 
} 
//图片加载
function loadImg(imgClass,imgSrc){
//data-src:存放需要加载的图片的路径
var arrImg=document.getElementsByClassName(imgClass); //需要实时加载class为imgClass的图片数组
for(var i=0; i<arrImg.length; i++){
if(!arrImg[i].getAttribute(imgSrc)) continue; 
if(document.documentElement.scrollTop+document.body.scrollTop+document.documentElement.clientHeight >arrImg[i].offsetTop){
arrImg[i].setAttribute("src",arrImg[i].getAttribute(imgSrc));
arrImg[i].removeAttribute(imgSrc);
}
}
}
window.onload=window.onscroll=function(){loadImg("aimg","data-src")}

</script>
</body>
</html>