<strong>原理和过程</strong> <br>1.页面滚动加载事件 <br>2.获取元素在页面里的top值 根据滚动条的位置 判断何时显示图片 <br>3.获取元素集合 加载过的图片从集合里删除 <br>效果预览:http://jsfiddle.net/dtdxrk/SkYNq/embedded/result/ <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="16254" class="copybut" id="copybut16254" onclick="doCopy('code16254')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code16254"> <br> <br> <br> <br><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <br><title>原生Js页面滚动延迟加载图片</title> <br><style type="text/css"> <BR>*{margin: 0;padding: 0} <BR>img.scrollLoading{border: 1px solid #ccc;display: block;margin-top: 10px;} <BR></style> <br> <br> <br><div id="content"></div> <br> <br> <br><script type="text/javascript"> <BR>var _CalF = { <BR>$ : function(object){//选择器 <BR>if(object === undefined ) return; <BR>var getArr = function(name,tagName,attr){ <BR>var tagName = tagName || '*', <BR>eles = document.getElementsByTagName(tagName), <BR>clas = (typeof document.body.style.maxHeight === "undefined") ? "className" : "class";//ie6 <BR>attr = attr || clas, <BR>Arr = []; <BR>for(var i=0;i<eles.length;i++){ <BR>if(eles[i].getAttribute(attr)==name){ <BR>Arr.push(eles[i]); <BR>} <BR>} <BR>return Arr; <BR>}; <BR>if(object.indexOf('#') === 0){ //#id <BR>return document.getElementById(object.substring(1)); <BR>}else if(object.indexOf('.') === 0){ //.class <BR>return getArr(object.substring(1)); <BR>}else if(object.match(/=/g)){ //attr=name <BR>return getArr(object.substring(object.search(/=/g)+1),null,object.substring(0,object.search(/=/g))); <BR>}else if(object.match(/./g)){ //tagName.className <BR>return getArr(object.split('.')[1],object.split('.')[0]); <BR>} <BR>}, <BR>getPosition : function(obj) { //获取元素在页面里的位置和宽高 <BR>var top = 0, <BR>left = 0, <BR>width = obj.offsetWidth, <BR>height = obj.offsetHeight; <BR>while(obj.offsetParent){ <BR>top += obj.offsetTop; <BR>left += obj.offsetLeft; <BR>obj = obj.offsetParent; <BR>} <BR>return {"top":top,"left":left,"width":width,"height":height}; <BR>} <BR>}; <br><br>//添加图片list <BR>var _temp = []; <BR>for (var i = 1; i < 21; i ++) { <BR>_temp.push('<img class="scrollLoading" data-src="http://images.cnblogs.com/cnblogs_com/Darren_code/311197/o_' + i + '.jpg" src="http://images.cnitblog.com/blog/150659/201306/23160223-c81dd9aa9a2a4071a47b0ced2c9118bc.gif" / alt="原生Js页面滚动延迟加载图片实现原理及过程_javascript技巧" ><br />图片' + i); <BR>} <BR>_CalF.$("#content").innerHTML = _temp.join(""); <br><br>function scrollLoad(){ <BR>this.init.apply(this, arguments); <BR>} <BR>scrollLoad.prototype ={ <BR>init : function(className){ <BR>var className = "img."+className, <BR>imgs = _CalF.$(className), <BR>that = this; <BR>this.imgs = imgs; <BR>that.loadImg(); <BR>window.onscroll = function(){ <BR>that.time = setTimeout(function(){ <BR>that.loadImg(); <BR>},400); <BR>} <BR>}, <BR>loadImg : function(){ <BR>var imgs = this.imgs.reverse(), //获取数组翻转 <BR>len = imgs.length; <BR>if(imgs.length===0){ <BR>clearTimeout(this.time); <BR>return; <BR>} <BR>for(var j=len-1;j>=0;j--){ //递减 <BR>var img = imgs[j], <BR>imgTop = _CalF.getPosition(img).top, <BR>imgSrc = img.getAttribute("data-src"), <BR>offsetPage = window.pageYOffset ? window.pageYOffset : window.document.documentElement.scrollTop,//滚动条的top值 <BR>bodyHeight = document.documentElement.clientHeight; //body的高度 <BR>if((offsetPage+bodyHeight/2) > (imgTop-bodyHeight/2)){ <BR>img.src = imgSrc; <BR>this.imgs.splice(j,1); <BR>} <BR>} <BR>} <BR>} <br><br>var img1 = new scrollLoad("scrollLoading"); <BR></script> <br> </div>