原生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)文章
JavaScript設(shè)計(jì)模式學(xué)習(xí)之適配器模式
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之適配器模式,對(duì)設(shè)計(jì)模式不熟悉的同學(xué),可以參考學(xué)習(xí)一下2021-04-04js+html5實(shí)現(xiàn)的自由落體運(yùn)動(dòng)效果代碼
這篇文章主要介紹了js+html5實(shí)現(xiàn)的自由落體運(yùn)動(dòng)效果,通過(guò)JavaScript結(jié)合html5元素調(diào)用時(shí)間函數(shù)實(shí)時(shí)計(jì)算實(shí)現(xiàn)物體自由下落及動(dòng)能損耗的效果,需要的朋友可以參考下2016-01-01BootStrap 圖標(biāo)icon符號(hào)圖標(biāo)glyphicons不正常顯示的快速解決辦法
這篇文章主要介紹了BootStrap 圖標(biāo)icon符號(hào)圖標(biāo)glyphicons不正常顯示的快速解決辦法,需要的朋友可以參考下2016-12-12前端如何利用CryptoJS實(shí)現(xiàn)數(shù)據(jù)信息的加密詳解
這篇文章主要給大家介紹了關(guān)于前端如何利用CryptoJS實(shí)現(xiàn)數(shù)據(jù)信息加密的相關(guān)資料,前端解密解密工具Cryptojs提供了前端加密解密的工作,包括常用的MD5、BASE64、SHA1、AES等加密解密方法,需要的朋友可以參考下2023-11-11記一次webpack3升級(jí)webpack4的踩坑經(jīng)歷
這篇文章主要介紹了記一次webpack3升級(jí)webpack4的踩坑經(jīng)歷,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06javascript 面向?qū)ο骹unction詳解及實(shí)例代碼
這篇文章主要介紹了javascript 面向?qū)ο骹unction詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02部分網(wǎng)站允許空白referer的防盜鏈圖片的js破解代碼
主要是有些網(wǎng)站的圖片調(diào)用是防盜鏈的但一般只是判斷referer是不是自己網(wǎng)站,如果referer為空也會(huì)顯示圖片,所以有了下面的代碼。2011-05-05