Skip to content

Instantly share code, notes, and snippets.

@bitfishxyz
Created January 19, 2020 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bitfishxyz/1e14a3fea57fb322f5ecd860bb951375 to your computer and use it in GitHub Desktop.
Save bitfishxyz/1e14a3fea57fb322f5ecd860bb951375 to your computer and use it in GitHub Desktop.
// getBoundingClientRect
let imgList1 = [...document.querySelectorAll(".get_bounding_rect")]
let num = imgList1.length
let lazyLoad1 = (function () {
let count = 0
return function () {
let deleteIndexList = []
imgList1.forEach((img,index) => {
let rect = img.getBoundingClientRect()
if (rect.top < window.innerHeight) {
img.src = img.dataset.src
// Add picture to delete list after loading successfully
deleteIndexList.push(index)
count++
if (count === num) {
//When all pictures are loaded, unbind scroll event
document.removeEventListener('scroll',lazyLoad1)
}
}
})
// Delete loaded pictures
imgList1 = imgList1.filter((_,index)=>!deleteIndexList.includes(index))
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment