Skip to content

Commit 96c86df

Browse files
authored
Merge pull request #13 from agenestar/main
Add script to identify candidates for lazy loading
2 parents 0b5880f + 83aed38 commit 96c86df

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ function findATFLazyLoadedImages() {
182182
console.log(findATFLazyLoadedImages());
183183
```
184184

185+
### Find non Lazy Loaded Images outside of the viewport
186+
187+
List all images that don't have `loading="lazy"` or `[data-src]` _(lazy loading via JS)_ and are not in the viewport when the page loads. This script will help you find candidates for lazy loading.
188+
189+
```js
190+
// Execute it after the page has loaded without any user interaction (Scroll, click, etc)
191+
function findImgCanidatesForLazyLoading() {
192+
let notLazyImages = document.querySelectorAll('img:not([data-src]):not([loading="lazy"])');
193+
return Array.from(notLazyImages).filter(tag => !isInViewport(tag));
194+
}
195+
196+
function isInViewport(tag) {
197+
let rect = tag.getBoundingClientRect();
198+
return (rect.bottom >= 0 &&
199+
rect.right >= 0 &&
200+
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
201+
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
202+
)
203+
}
204+
205+
console.log("Consider lazyloading the following images: ", findImgCanidatesForLazyLoading());
206+
```
207+
185208
### Image Info
186209

187210
List all image resources and sort by (`name, transferSize, encodedBodySize, decodedBodySize, initiatorType`)

0 commit comments

Comments
 (0)