原生javascript實(shí)現(xiàn)圖片滾動(dòng)、延時(shí)加載功能
實(shí)現(xiàn)效果:下拉滾動(dòng)條時(shí),圖片出現(xiàn)在可見區(qū)域時(shí),才開始加載
思路:
(1)img標(biāo)簽,把真實(shí)的圖片地址,放在自己設(shè)置的屬性里面,如 lazy-src
(2)獲取img離頁(yè)面的高度(在JQ里是offset().top),原生是:
img.getBoundingClientRect().top + document.body.scrollTop||document.documentElement.scrollTop
(3)判斷img出現(xiàn)的位置是否在可見區(qū)域里:
.在瀏覽器的可見區(qū)域,justTop>scrollTop&&offsetTop<(scrollTop+windowHeight),這里的justTop是圖片的offsetTop+圖片高度
//保存document在變量里,減少對(duì)document的查詢
var doc = document;
for(var n=0,i = this.oImg.length;n<i;n++){
//獲取圖片占位符圖片地址
var hSrc = this.oImg[n].getAttribute(this.sHolder_src);
if(hSrc){
var scrollTop = doc.body.scrollTop||doc.documentElement.scrollTop,
windowHeight = doc.documentElement.clientHeight,
offsetTop = this.oImg[n].getBoundingClientRect().top + scrollTop,
imgHeight = this.oImg[n].clientHeight,
justTop = offsetTop + imgHeight;
// 判斷圖片是否在可見區(qū)域
if(justTop>scrollTop&&offsetTop<(scrollTop+windowHeight)){
this.isLoad(hSrc,n);
}
}
}
以下為詳細(xì)代碼:
function LGY_imgScrollLoad(option){
this.oImg = document.getElementById(option.wrapID).getElementsByTagName('img');
this.sHolder_src = option.holder_src;
this.int();
}
LGY_imgScrollLoad.prototype = {
loadImg:function(){
//保存document在變量里,減少對(duì)document的查詢
var doc = document;
for(var n=0,i = this.oImg.length;n<i;n++){
//獲取圖片占位符圖片地址
var hSrc = this.oImg[n].getAttribute(this.sHolder_src);
if(hSrc){
var scrollTop = doc.body.scrollTop||doc.documentElement.scrollTop,
windowHeight = doc.documentElement.clientHeight,
offsetTop = this.oImg[n].getBoundingClientRect().top + scrollTop,
imgHeight = this.oImg[n].clientHeight,
justTop = offsetTop + imgHeight;
// 判斷圖片是否在可見區(qū)域
if(justTop>scrollTop&&offsetTop<(scrollTop+windowHeight)){
//alert(offsetTop);
this.isLoad(hSrc,n);
}
}
}
},
isLoad:function(src,n){
var src = src,
n = n,
o_img = new Image(),
_that = this;
o_img.onload = (function(n){
_that.oImg[n].setAttribute('src',src);
_that.oImg[n].removeAttribute(_that.sHolder_src);
})(n);
o_img.src = src;
},
int:function(){
this.loadImg();
var _that = this,
timer = null;
// 滾動(dòng):添加定時(shí)器,防止頻繁調(diào)用loadImg函數(shù)
window.onscroll = function(){
clearTimeout(timer);
timer = setTimeout(function(){
_that.loadImg();
},100);
}
}
}
效果圖:

以上就是本文的全部?jī)?nèi)容了,實(shí)現(xiàn)的效果不比jQuery插件實(shí)現(xiàn)的差吧,代碼還簡(jiǎn)潔,小伙伴們參考下吧。
相關(guān)文章
iframe窗口高度自適應(yīng)的實(shí)現(xiàn)方法
這篇文章主要介紹了iframe窗口高度自適應(yīng)的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01
JavaScript在IE和Firefox上的差異及相互替代的實(shí)現(xiàn)方法
我們經(jīng)常在處理ie和firefox下的js總會(huì)碰到一些兼容問題,下面是些總結(jié),希望大家仔細(xì)看看研究2008-06-06
artDialog+plupload實(shí)現(xiàn)多文件上傳
這篇文章主要介紹了artDialog+plupload實(shí)現(xiàn)多文件上傳的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
微信小程序之滑動(dòng)頁(yè)面隱藏和顯示組件功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序之滑動(dòng)頁(yè)面隱藏和顯示組件功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
JavaScript實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
如何用threejs實(shí)現(xiàn)實(shí)時(shí)多邊形折射
這篇文章主要介紹了如何用threejs實(shí)現(xiàn)實(shí)時(shí)多邊形折射,對(duì)three.js庫(kù)感興趣的同學(xué),可以參考下2021-05-05

