/* 1、调用方式 new LazyLoad(占位图[, rel的属性名]); 2、如果第二个参数存在, 则只访问 rel = 最二个参数的图片 */ function LazyLoad(empty_img, attr){ var nu = navigator.userAgent; if (!/chrome|firefox/i.test(nu)) { this.index(empty_img, attr); } } LazyLoad.prototype={ index:function (empty_img, rel){ var images = document.images, list=[], sys=this; var dr=this.getRect(); for (var i = images.length; i--;) { var o = images[i], or=this.getRect(o); if (o.getAttribute('rel') == rel || or.top0) { function checkAndLoad(){ if(list.length==0){return} clearTimeout(sys.lazyTimer); sys.lazyTimer=setTimeout(function() { sys.loadImg() },200) } checkAndLoad(); this.on(window,'scroll', checkAndLoad); this.on(window,'resize', checkAndLoad); } }, get:function (el){ return typeof el=="string" ? document.getElementById(el) : el; }, on: function(el, type, fn) { el.attachEvent ? el.attachEvent('on' + type,function() { fn.call(el, event) }) : el.addEventListener(type, fn, false); }, loadImg:function (){ var list=this.list, noload=[], dr=this.getRect(), S = this; for (var i = list.length; i--;) { var o=list[i]; if (this.isIntersect(dr, this.getRect(o))) { o.onload = function (){ S.fadeIn(this); this.style.visibility='visible'; } o.src=o.getAttribute('rel'); continue; } noload.push(o) } this.list=noload }, isIntersect:function (r1, r2){ return !(r1.rightr2.bottom||r1.left>r2.right||r1.bottom