Created
January 19, 2020 03:13
-
-
Save bitfishxyz/62a2d99a237ed1debfab54b8748615c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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