原生js頁面滾動延遲加載圖片
更新時間:2015年12月20日 09:11:55 作者:一落葉而知秋
這篇文章主要介紹了原生js頁面滾動延遲加載圖片的相關(guān)資料,現(xiàn)在瀑布流效果大行其道,各種網(wǎng)站都有應(yīng)用,尤其是專業(yè)的圖片類型的網(wǎng)站,感興趣的朋友可以參考下
本文實例為大家講解了javascript瀑布流代碼,即js頁面滾動延遲加載圖片,分享給大家供大家參考,具體代碼如下
<!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="http://images.cnblogs.com/cnblogs_com/Darren_code/311197/o_' + i + '.jpg" src="http://images.cnitblog.com/blog/150659/201306/23160223-c81dd9aa9a2a4071a47b0ced2c9118bc.gif" /><br />圖片' + 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(), //獲取數(shù)組翻轉(zhuǎn)
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>
希望本文所述對大家學(xué)習(xí)javascript程序設(shè)計有所幫助。
相關(guān)文章
由淺入深講解Javascript繼承機制與simple-inheritance源碼分析
Javascript語言對繼承實現(xiàn)的并不好,需要工程師自己去實現(xiàn)一套完整的繼承機制。下面我們由淺入深的系統(tǒng)掌握使用javascript繼承的技巧,對javascript繼承相關(guān)知識感興趣的朋友一起看看吧2015-12-12
TypeScript實現(xiàn)數(shù)組和樹的相互轉(zhuǎn)換
樹或者圖是個比較抽象的概念,并不存在這樣的數(shù)據(jù)類型。數(shù)組就比較簡單了,因此數(shù)組和樹的轉(zhuǎn)換可以理解為數(shù)組和對象之間的轉(zhuǎn)換。本文將用TypeScript實現(xiàn)數(shù)組和樹的相互轉(zhuǎn)換,感興趣的可以了解一下2022-06-06
Echarts動態(tài)加載多條折線圖的實現(xiàn)代碼
這篇文章主要介紹了Echarts動態(tài)加載多條折線圖的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05

