Skip to content

Instantly share code, notes, and snippets.

@tdtgit

tdtgit/lazyload-datuan.js

Last active Nov 18, 2020
Embed
What would you like to do?
if ('loading' in document.createElement('img')) {
document.querySelectorAll('[loading="lazy"]').forEach(elem => {
if (elem.dataset.src) {
elem.src = elem.dataset.src;
}
if (elem.dataset.srcset) {
elem.srcset = elem.dataset.srcset;
}
})
} else {
document.querySelectorAll('[loading=lazy]').forEach(elem => {
elem.classList.add("lazyload");
})
var script = document.createElement('script');
script.onload = function () {
const observer = lozad();
observer.observe();
};
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js';
document.head.appendChild(script);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment