Load all images when intersection observer is not supported

awakening

I'm using intersection observer for late loading images. How can I load all images in browsers that don't support intersection observer?

My script=

const imob = new IntersectionObserver((entries, self) => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            llo(entry.target);
            self.unobserve(entry.target);
        }
    });
});
document.querySelectorAll('.lzyp').forEach((pcu) => {
    imob.observe(pcu);
});

const llo = (pcu) => {
    const img = pcu.querySelector('img');
    const sce = pcu.querySelectorAll('source');

    sce.forEach((sue) => {
        sue.srcset = sue.dataset.srcset;
        sue.removeAttribute('data-srcset');
    });
    img.src = img.dataset.src;
    img.removeAttribute('data-src');
}
Leyiang
  1. Maybe a polyfill for IntersectionObserver?

  2. Adding fallback code if IntersectionObserver is not supported.

if (!('IntersectionObserver' in window) ||
    !('IntersectionObserverEntry' in window) ||
    !('intersectionRatio' in window.IntersectionObserverEntry.prototype) ||
    !('isIntersecting' in window.IntersectionObserverEntry.prototype)
) {
    // load all images here
    document.querySelectorAll('.lzyp').forEach( llo );
}

Resolve Arrow function is not supported in IE:

// change from this
document.querySelectorAll('.lzyp').forEach((pcu) => {
    imob.observe(pcu);
});

// to this
document.querySelectorAll('.lzyp').forEach( function(pcu) {
    imob.observe(pcu);
});

put together:

function llo(pcu) {
    const img = pcu.querySelector('img');
    const sce = pcu.querySelectorAll('source');

    sce.forEach((sue) => {
        sue.srcset = sue.dataset.srcset;
        sue.removeAttribute('data-srcset');
    });
    img.src = img.dataset.src;
    img.removeAttribute('data-src');
}

// IntersectionObserver is not supported
if (!('IntersectionObserver' in window) ||
    !('IntersectionObserverEntry' in window) ||
    !('intersectionRatio' in window.IntersectionObserverEntry.prototype) ||
    !('isIntersecting' in window.IntersectionObserverEntry.prototype)
) {
    // load all images here
    document.querySelectorAll('.lzyp').forEach( llo );
} else {
    const imob = new IntersectionObserver( function(entries, self) {
        entries.forEach( function(entry) {
            if (entry.isIntersecting) {
                llo(entry.target);
                self.unobserve(entry.target);
            }
        });
    });
    document.querySelectorAll('.lzyp').forEach(function(pcu) {
        imob.observe(pcu);
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to lazy load images using intersection observer?

Intersection observer "unloads" images after looping on carousel (Siema)

How to lazy load html div tags using intersection-observer?

Unable to clear interval when element is outside viewport in Intersection Observer API

Execute a function when a div is visible, using the intersection observer

React : render a component only when they are visible ( react-intersection-observer )

Run function when element enters to viewport with getBoundingClientRect or intersection observer

Intersection Observer trigger when element is visible (before scrolling)

CSS Animations laggy or jumpy when using Intersection Observer API

Why is Intersection Observer adding class to all observed elements upon intersection of first element?

intersection observer multiple animation

Intersection Observer is not trustable?

Animating with Intersection Observer

Intersection Observer not working with siblings

Intersection Observer API is not available

Intersection observer not working as expected

Intersection observer for jquery

Load all images inside a class

Font Face Observer loading all fonts on page load?

single intersection observer for multiple entries

Intersection observer with child component in react

Mock for intersection observer in jest and typescript

Problem with Intersection Observer API and Bootstrap

Intersection Observer in Edge / Problem in the animation

Change the all images webp file extension on Safari browser when my website load

Use Intersection Observer to add different classes to elements when scrolled into view from top or bottom

What's causing Intersection Observer API to respond differently when replacing synchronous logic with asynchronous logic?

How to load all the images in the background of a RecyclerView in Android

How to load all the Images using Media Store?