// Для images
(function () {
const images = document.querySelectorAll('img');
const options = {
root: null,
rootMargin: '0px',
threshold: 0.1
}
function handleImg(myImg, observer) {
myImg.forEach(myImgSingle => {
if (myImgSingle.intersectionRatio > 0) {
loadImage(myImgSingle.target);
}
})
}
function loadImage(image) {
image.src = image.getAttribute('data');
}
const observer = new IntersectionObserver(handleImg, options);
images.forEach(img => {
observer.observe(img);
})
})();
// Для source
(function () {
const source = document.querySelectorAll('source'); // дёргаем все source, нужно будет уточнить :)
const options = {
root: null,
rootMargin: '0px',
threshold: 0.1
}
function handleSource(mySource, observer) {
mySource.forEach(mySourceSingle => {
if (mySourceSingle.intersectionRatio > 0) {
loadImage(mySourceSingle.target);
}
})
}
function loadImage(source) {
source.srcset = source.getAttribute('data');
}
let observer = new IntersectionObserver(handleSource, options);
source.forEach(source => {
observer.observe(source);
})
})();