合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
* 图片全部加载完成后移除事件监听; * 加载完的图片,从 imgList 移除; ~~~ let imgList = [...document.querySelectorAll('img')] let length = imgList.length // 修正错误,需要加上自执行 const imgLazyLoad = (function() { let count = 0 return function() { let deleteIndexList = [] imgList.forEach((img, index) => { let rect = img.getBoundingClientRect() if (rect.top < window.innerHeight) { img.src = img.dataset.src deleteIndexList.push(index) count++ if (count === length) { document.removeEventListener('scroll', imgLazyLoad) } } }) imgList = imgList.filter((img, index) => !deleteIndexList.includes(index)) } })() // 这里最好加上防抖处理 document.addEventListener('scroll', imgLazyLoad) ~~~