原理和过程
1.页面滚动加载事件
2.获取元素在页面里的top值 根据滚动条的位置 判断何时显示图片
3.获取元素集合 加载过的图片从集合里删除
效果预览:http://jsfiddle.net/dtdxrk/SkYNq/embedded/result/
复制代码 代码如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>原生Js页面滚动延迟加载图片</title>
<style type="text/css">
*{margin: 0;padding: 0}
img.scrollLoading{border: 1px solid #ccc;display: block;margin-top: 10px;}
</style>
</head>
<body>
<div id="content"></div>
</body>
</html>
<script type="text/javascript">
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('<img class="scrollLoading" data-src="/UploadFiles/2021-04-02/o_' + i + '.jpg">}
_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");
</script>
1.页面滚动加载事件
2.获取元素在页面里的top值 根据滚动条的位置 判断何时显示图片
3.获取元素集合 加载过的图片从集合里删除
效果预览:http://jsfiddle.net/dtdxrk/SkYNq/embedded/result/
复制代码 代码如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>原生Js页面滚动延迟加载图片</title>
<style type="text/css">
*{margin: 0;padding: 0}
img.scrollLoading{border: 1px solid #ccc;display: block;margin-top: 10px;}
</style>
</head>
<body>
<div id="content"></div>
</body>
</html>
<script type="text/javascript">
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('<img class="scrollLoading" data-src="/UploadFiles/2021-04-02/o_' + i + '.jpg">}
_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");
</script>
标签:
页面滚动,延迟加载
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“原生Js页面滚动延迟加载图片实现原理及过程”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。