forked from hamid/intersection-observer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource-prioritization.js
More file actions
40 lines (38 loc) · 1.15 KB
/
resource-prioritization.js
File metadata and controls
40 lines (38 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
document.addEventListener('DOMContentLoaded', () => {
let lazyImages = document.querySelectorAll('.lazy-load');
let imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
let img = entry.target;
img.src = img.getAttribute('data-src');
observer.unobserve(img); // Stop observing the image once it's loaded.
} else {
// Adjust priority based on connection speed or other criteria
adjustLoadingPriority(entry.target);
}
});
}, {
rootMargin: '500px 0px', // Start loading images 500px before they enter the viewport
threshold: 0.01
});
lazyImages.forEach(img => {
imageObserver.observe(img);
});
});
function adjustLoadingPriority(img) {
if (navigator.connection) {
switch (navigator.connection.effectiveType) {
case '4g':
// High priority - load immediately
img.src = img.getAttribute('data-src');
break;
case '3g':
// Medium priority - maybe load
break;
case '2g':
case 'slow-2g':
// Low priority - delay loading
break;
}
}
}