web前端|js教程
页面滚动,延迟加载
web前端-js教程
原理和过程
1.页面滚动加载事件
2.获取元素在页面里的top值 根据滚动条的位置 判断何时显示图片
3.获取元素集合 加载过的图片从集合里删除
效果预览:/dtdxrk/SkYNq/embedded/result/
图集下载php源码,树莓 ubuntu阿里源,爬虫项目下载,php-dom,seo外包攻略lzw
asp源码学习,vscode 浏览器产看,ubuntu 无hgfs,tomcat的整体架构,xpath爬虫例子,php源码使用教程,seo优化推广法有什么特点,游戏网站 源码lzw
*{margin: 0;padding: 0}
img.scrollLoading{border: 1px solid #ccc;display: block;margin-top: 10px;}
上网认证源码,vscode 技巧视频,ubuntu更改ip地址,tomcat path=,sqlite只读属性,如何访问dns服务器,ecshop真假订单验证插件,优酷前端框架,python爬虫如何运行,php果酱,如何seo优化排名,网站在线qq客服代码,精美网页模版,pc商城模板,js 页面导航菜单,云集管理系统,程序模块地址lzw
var _CalF = {
$ : function(object){//选择器
if(object === undefined ) return;
var getArr = function(name,tagName,attr){
var tagName = tagName || ‘*’,
eles = document.getElementsByTagName(tagName),
clas = (typeof document.body.style.maxHeight === “undefined”) ? “className” : “class”;//ie6
attr = attr || clas,
Arr = [];
for(var i=0;i<eles.length;i++){
if(eles[i].getAttribute(attr)==name){
Arr.push(eles[i]);
}
}
return Arr;
};
if(object.indexOf(‘#’) === 0){ //#id
return document.getElementById(object.substring(1));
}else if(object.indexOf(‘.’) === 0){ //.class
return getArr(object.substring(1));
}else if(object.match(/=/g)){ //attr=name
return getArr(object.substring(object.search(/=/g)+1),null,object.substring(0,object.search(/=/g)));
}else if(object.match(/./g)){ //tagName.className
return getArr(object.split(‘.’)[1],object.split(‘.’)[0]);
}
},
getPosition : function(obj) { //获取元素在页面里的位置和宽高
var top = 0,
left = 0,
width = obj.offsetWidth,
height = obj.offsetHeight;
while(obj.offsetParent){
top += obj.offsetTop;
left += obj.offsetLeft;
obj = obj.offsetParent;
}
return {“top”:top,”left”:left,”width”:width,”height”:height};
}
};
//添加图片list
var _temp = [];
for (var i = 1; i < 21; i ++) {
_temp.push(‘
图片’ + i);
}
_CalF.$(“#content”).innerHTML = _temp.join(“”);
function scrollLoad(){
this.init.apply(this, arguments);
}
scrollLoad.prototype ={
init : function(className){
var className = “img.”+className,
imgs = _CalF.$(className),
that = this;
this.imgs = imgs;
that.loadImg();
window.onscroll = function(){
that.time = setTimeout(function(){
that.loadImg();
},400);
}
},
loadImg : function(){
var imgs = this.imgs.reverse(), //获取数组翻转
len = imgs.length;
if(imgs.length===0){
clearTimeout(this.time);
return;
}
for(var j=len-1;j>=0;j–){ //递减
var img = imgs[j],
imgTop = _CalF.getPosition(img).top,
imgSrc = img.getAttribute(“data-src”),
offsetPage = window.pageYOffset ? window.pageYOffset : window.document.documentElement.scrollTop,//滚动条的top值
bodyHeight = document.documentElement.clientHeight; //body的高度
if((offsetPage+bodyHeight/2) > (imgTop-bodyHeight/2)){
img.src = imgSrc;
this.imgs.splice(j,1);
}
}
}
}
var img1 = new scrollLoad(“scrollLoading”);