码迷,mamicode.com
首页 > 其他好文 > 详细

图片懒加载

时间:2015-10-14 12:16:26      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

(function(){
//common
function tagName(tagName){
return document.getElementsByTagName(tagName);
}
function $(id){
return document.getElementById(id);
}
function addEvent(obj,type,func){
if(obj.addEventListener){
obj.addEventListener(type,func,false);
}else if(obj.attachEvent){
obj.attachEvent(‘on‘+type,func);
}
}

//建立某些参数
var v={
eleGroup:null,
eleTop:null,
eleHeight:null,
screenHeight:null,
visibleHeight:null,
scrollHeight:null,
scrolloverHeight:null,
limitHeight:null
}

//对数据进行初始化
function init(element){
v.eleGroup=tagName(element)
screenHeight=document.documentElement.clientHeight;
scrolloverHeight=document.body.scrollTop;
for(var i=0,j=v.eleGroup.length;i<j;i++){
if(v.eleGroup[i].offsetTop<=screenHeight && v.eleGroup[i].getAttribute(‘asrc‘)){
v.eleGroup[i].setAttribute(‘src‘,v.eleGroup[i].getAttribute(‘asrc‘));
v.eleGroup[i].removeAttribute(‘asrc‘)
}
}

}
function lazyLoad(){
if(document.body.scrollTop == 0){
limitHeight=document.documentElement.scrollTop+document.documentElement.clientHeight;
}else{
limitHeight=document.body.scrollTop+document.documentElement.clientHeight;
}
for(var i=0,j=v.eleGroup.length;i<j;i++){
if(v.eleGroup[i].offsetTop<=limitHeight && v.eleGroup[i].getAttribute(‘asrc‘)){
v.eleGroup[i].src=v.eleGroup[i].getAttribute(‘asrc‘);
v.eleGroup[i].removeAttribute(‘asrc‘)
}
}
}
init(‘img‘)
addEvent(window,‘scroll‘,lazyLoad);
})()

图片懒加载

标签:

原文地址:http://www.cnblogs.com/GoodPingGe/p/4876800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!