Created
January 19, 2020 04:10
-
-
Save bitfishxyz/1e14a3fea57fb322f5ecd860bb951375 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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