我是靠谱客的博主 粗心狗,最近开发中收集的这篇文章主要介绍js图片实时加载提供网页打开速度,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

其实实时加载图片思路很简单,页面一开始加载的时候把不需要一开始就加载的图片(如第二屏以下的图片,反正是要往下滚动才能看到的,不如把它们设置成往下滚动的时候再实时加载)路径保持到一个自定义的属性里,如:<img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012929844.jpg" alt="19岁女孩直面癌症 7年前曾截肢" />,src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif"是一个1px*1px的透明gif图片,自定义属性data-src里的"http://www.uoften.com/uploadfile/2018/0411/20180411012929844.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://www.uoften.com/uploadfile/2018/0411/20180411012929366.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://www.uoften.com/uploadfile/2018/0411/20180411012929331.jpg" alt="19岁女孩直面癌症 7年前曾截肢" /></a></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012930827.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012930650.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012930484.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012930885.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012930122.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012930989.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012931158.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012931664.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012931420.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012931933.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012931821.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012929844.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012931315.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012932231.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012932903.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012932758.jpg" alt="19岁女孩直面癌症 7年前曾截肢" /></a></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012933935.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012933315.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012933871.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012933503.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012933470.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012933981.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012934360.jpg" /></li>
</ul>
</div>
<div class="cont3 wp">
<h2>cont3</h2>
<ul>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012934141.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012934917.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012934288.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012934240.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012934207.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012934223.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012934345.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012935934.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012935784.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012935386.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012935112.jpg" /></li>
<li><img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012935614.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://www.uoften.com/uploadfile/2018/0411/20180411012929366.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012935386.jpg" width="310" height="428" />
<p>http://www.cnblogs.com/xiaomou2014</p>
<img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012935112.jpg" width="310" height="419" />
<p>http://www.cnblogs.com/xiaomou2014</p>
<img class="aimg" src="http://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012935614.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012929844.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012931315.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012932231.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012932903.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://www.uoften.com/uploadfile/2018/0411/20180411012929877.gif" data-src="http://www.uoften.com/uploadfile/2018/0411/20180411012932758.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>

最后

以上就是粗心狗为你收集整理的js图片实时加载提供网页打开速度的全部内容,希望文章能够帮你解决js图片实时加载提供网页打开速度所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(103)

评论列表共有 0 条评论

立即
投稿
返回
顶部