The class attribute, which is what we'll select the element with in JavaScript.
The src attribute, which references a placeholder image that will appear when the page first loads.
The data-src and data-srcset attributes, which are placeholder attributes containing the URL for the image we'll load once the element is in the viewport.
document.addEventListener("DOMContentLoaded", function() {
var lazyImages = [].slice.call(document.querySelectorAll("img.lazy"));
if ("IntersectionObserver" in window) {
let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
let lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
lazyImage.srcset = lazyImage.dataset.srcset;
lazyImage.classList.remove("lazy");
lazyImageObserver.unobserve(lazyImage);
}
});
});
lazyImages.forEach(function(lazyImage) {
lazyImageObserver.observe(lazyImage);
});
} else {
// Possibly fall back to a more compatible method here
}
});
Setting the preload attribute to metadata indicates that the user is not expected to need the video, but that fetching its metadata (dimensions, track list, duration, and so on) is desirable.
auto
Setting the preload attribute to auto indicates that the browser may cache enough data that complete playback is possible without requiring a stop for further buffering.
Link preload
Resources loaded via <link rel="preload"> are stored locally in the browser, and are effectively inert until they're explicitly referenced in the DOM, JavaScript, or CSS.
Preload is different from prefetch in that it focuses on current navigation and fetches resources with priority based on their type (script, style, font, video, audio, etc.). It should be used to warm up the browser cache for current sessions.
<link rel="preload" as="video" href="https://cdn.com/small-file.mp4">
<video id="video" controls></video>
<script>
// Later on, after some condition has been met, set video source to the
// preloaded video URL.
video.src = 'https://cdn.com/small-file.mp4';
video.play().then(_ => {
// If preloaded video URL was already cached, playback started immediately.
});
</script>
Lazy Loading Images and Video | Web Fundamentals | Google Developers
Using intersection observer
IntersectionObserver API 使用教程 - 阮一峰的网络日志
IntersectionObserver.IntersectionObserver() - Web API 接口 | MDN
Lazy Loading with Intersection Observer Example
Using event handlers (the most compatible way)
Element.getBoundingClientRect() - Web API 接口 | MDN
Lazy Loading Example
Images in CSS
Lazy Loading CSS Background Images with Intersection Observer
Lazy loading video
Fast Playback with Video Preload | Web Fundamentals | Google Developers
metadata
Setting the preload attribute to metadata indicates that the user is not expected to need the video, but that fetching its metadata (dimensions, track list, duration, and so on) is desirable.
auto
Setting the preload attribute to auto indicates that the browser may cache enough data that complete playback is possible without requiring a stop for further buffering.
Link preload
Resources loaded via
<link rel="preload">
are stored locally in the browser, and are effectively inert until they're explicitly referenced in the DOM, JavaScript, or CSS.Preload is different from prefetch in that it focuses on current navigation and fetches resources with priority based on their type (script, style, font, video, audio, etc.). It should be used to warm up the browser cache for current sessions.