我创建了一个脚本,该脚本利用带有webp和jpg后备广告的元素以及一些延迟加载来提供非常高质量的图像。到目前为止,它可以在除iOS11 Safari之外的所有“现代”浏览器中正常运行。
我已经为IntersectionObserver实现了一个后备,因为我知道尚不完全支持它,它使用getClientBoundingRect()查找图像在视口中的位置。我已经检查过iOS 10、11,然后将其中一台设备升级到开始加载图像的iOS 12。
<div class="lazy-container">
<picture class="lazy-thumbnail">
<source srcset="{{ asset( '/images/media/placeholders/3.webp' ) }}" type="image/webp">
<source srcset="{{ asset( '/images/media/placeholders/3.jpg' ) }}" type="image/jpeg">
<img class="" src="{{ asset( '/images/media/placeholders/3.jpg' ) }}" />
</picture>
<picture data-lazy>
<source data-srcset="{{ asset( '/images/media/3.webp' ) }}" type="image/webp">
<source data-srcset="{{ asset( '/images/media/3.jpg' ) }}" type="image/jpeg">
<img class="" src="{{ asset( '/images/media/3.jpg' ) }}" />
</picture>
</div>
const lazyObserver = new IntersectionObserver( ( entries, observer ) => {
// Break into function to allow immediate calling
const exchangeImage = ( image, source ) => {
source.forEach( ( s ) => {
s.setAttribute( 'srcset',
s.getAttribute( 'data-srcset' ) );
});
image.previousElementSibling.classList.remove( 'lazy-thumbnail' );
image.previousElementSibling.classList.add( 'ex-lazy-thumbnail' );
image.classList.add( 'ex-lazy' );
image.removeAttribute( 'data-lazy' );
// If the image is in view and src has been swapped out, stop observing the image
lazyObserver.unobserve( image );
};
entries.forEach( ( entry ) => {
const image = entry.target;
const source = image.querySelectorAll( 'source' );
// If the image is either in view or data-lazy='immediate', load the image.
if( entry.target.dataset.lazy === 'immediate' || entry.isIntersecting ){
exchangeImage( image, source );
}
});
});
picture元素应经过并加载适用于浏览器的第一个图像srcset,在大多数情况下为webp,然后选择这些JS时,应在查看图像时运行JS,将srcset换为data-srcset具有较大的图像和负载。这适用于除浏览器以外的所有浏览器
.lazy-container {
overflow: hidden;
position: relative;
picture {
width: 100%;
position: absolute;
top: 0;
left: 0;
img {
display: block;
width: 100%;
height: auto;
}
}
}
.ex-lazy {
filter: blur(0px);
transition: all 500ms ease-in-out;
}
.lazy-thumbnail {
opacity: 1;
filter: blur(8px);
transition: all 500ms ease-in-out;
z-index: 1;
}
.ex-lazy-thumbnail {
opacity: 0;
filter: blur(0px);
transition: all 500ms ease-in-out;
z-index: 0;
}
[data-lazy]{
width: 100%;
height: auto;
filter: blur(8px);
transition: all 500ms ease-in-out;
}
答案 0 :(得分:1)
我用自己的方式解决了这个问题。 另外看看polyfill可以帮助polyfill
let images = document.querySelectorAll('source, img');
if ('IntersectionObserver' in window) {
// we check if IntersectionObserver is supported by our browsers
let config = {
root: null,
rootMargin: '0px',
threshold: 0.5
};
let observer = new IntersectionObserver(onChange, config);
images.forEach(function (img) { observer.observe(img) });
function onChange(changes, observer) {
changes.forEach(function (change) {
if (change.intersectionRatio > 0) {
// we stop observing and loading pictures
loadImage(change.target);
observer.unobserve(change.target);
}
});
}
} else {
// if IntersectionObserver is not supported, we load all photos
images.forEach(function (image) { loadImage(image) });
}
function loadImage(image) {
image.classList.add('fade-in');
if (image.dataset && image.dataset.src) {
image.src = image.dataset.src;
}
if (image.dataset && image.dataset.srcset) {
image.srcset = image.dataset.srcset;
}
}
// -- How to add polyfil
const modernBrowser = ('IntersectionObserver' in window);
if (!modernBrowser) {
loadScripts([
"./polyfills/intersection-observer.js"
])
}
function loadScripts(array, callback) {
var loader = function (src, handler) {
var script = document.createElement("script");
script.src = src;
script.onload = script.onreadystatechange = function () {
script.onreadystatechange = script.onload = null;
handler();
}
var head = document.getElementsByTagName("head")[0];
(head || document.body).appendChild(script);
};
(function run() {
if (array.length != 0) {
loader(array.shift(), run);
} else {
callback && callback();
}
})();
}
<picture>
<source media="(min-width: 400px)" data-srcset="https://place-hold.it/400x400/15252D/fff">
<source media="(min-width: 200px)" data-srcset="https://place-hold.it/200x200/15252D/fff">
<img data-src="https://place-hold.it/100x100/15252D/fff">
<noscript><img src="https://place-hold.it/100x100/15252D/fff"></noscript>
</picture>