Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 57d544e

Browse filesBrowse files
committed
Improve the Lazy Loading scripts
1 parent 5a2a0b0 commit 57d544e
Copy full SHA for 57d544e

File tree

Expand file treeCollapse file tree

2 files changed

+38
-19
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+38
-19
lines changed
+16-8Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
# Find Above The Fold Lazy Loaded Images
22

3-
List all images that have `loading="lazy"` or `[data-src]` _(lazy loading via JS)_ above the fold
3+
List all images that have `loading="lazy"` or `[data-src]` _(lazy loading via JS)_ above the fold and off canvas.
44

55
#### Snippet
66

77
```js copy
88
// List all lazily loaded images above the fold
99
// https://webperf-snippets.nucliweb.net
10-
function findATFLazyLoadedImages() {
11-
const lazy = document.querySelectorAll('[loading="lazy"], [data-src]');
10+
function findAboveTheFoldLazyLoadedImages() {
11+
const lazy = document.querySelectorAll('[loading="lazy"]');
1212
let lazyImages = [];
13+
1314
lazy.forEach((tag) => {
15+
const {x, y} = tag.getBoundingClientRect();
1416
const position = parseInt(tag.getBoundingClientRect().top);
15-
if (position < window.innerHeight && position !== 0) {
17+
18+
if(x < window.innerWidth && y < window.innerHeight && x !== 0 && y !== 0) {
1619
lazyImages = [...lazyImages, tag];
20+
console.log(tag);
1721
}
1822
});
19-
return lazyImages.length > 0
20-
? lazyImages
21-
: "Good job, the site does not have any lazily loaded images in the viewport.";
23+
24+
if( lazyImages.length === 0 ) {
25+
console.log(
26+
`%c Good job, the site does not have any lazily loaded images in the viewport.`,
27+
"background: #222; color: lightgreen; padding: 0.5ch",
28+
);
29+
}
2230
}
2331

24-
console.log(findATFLazyLoadedImages());
32+
findAboveTheFoldLazyLoadedImages();
2533
```

‎pages/Loading/Find-non-Lazy-Loaded-Images-outside-of-the-viewport.mdx

Copy file name to clipboardExpand all lines: pages/Loading/Find-non-Lazy-Loaded-Images-outside-of-the-viewport.mdx
+22-11Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ List all images that don't have `loading="lazy"` or `[data-src]` _(lazy loading
66

77
```js copy
88
// Execute it after the page has loaded without any user interaction (Scroll, click, etc)
9-
function findImgCanidatesForLazyLoading() {
10-
let notLazyImages = document.querySelectorAll(
11-
'img:not([data-src]):not([loading="lazy"])',
12-
);
13-
return Array.from(notLazyImages).filter((tag) => !isInViewport(tag));
14-
}
15-
169
function isInViewport(tag) {
1710
let rect = tag.getBoundingClientRect();
1811
return (
@@ -23,8 +16,26 @@ function isInViewport(tag) {
2316
);
2417
}
2518

26-
console.log(
27-
"Consider lazyloading the following images: ",
28-
findImgCanidatesForLazyLoading(),
29-
);
19+
function findImgCanidatesForLazyLoading() {
20+
let notLazyImages = document.querySelectorAll(
21+
'img:not([data-src]):not([loading="lazy"])'
22+
);
23+
const notLazyImagesOutOfViewport = Array.from(notLazyImages).filter((tag) => !isInViewport(tag));
24+
25+
if( notLazyImagesOutOfViewport.length === 0 ) {
26+
console.log(
27+
`%c Good job, the site has al the images out of the viewport with lazyloading.`,
28+
"background: #222; color: lightgreen; padding: 0.5ch",
29+
);
30+
} else {
31+
console.log(
32+
`%c Consider lazyloading the following images`,
33+
"background: #222; color: lightcoral; padding: 0.5ch; margin-top: 1em",
34+
);
35+
notLazyImagesOutOfViewport.forEach((img) => console.log(img));
36+
}
37+
38+
}
39+
40+
findImgCanidatesForLazyLoading();
3041
```

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.