图片懒加载
懒加载(Lazy Loading)是一种计算机编程技术,用于延迟初始化对象或资源,直到它们实际需要使用时才进行加载或初始化。这种技术可以提高程序的启动速度和性能,减少不必要的资源消耗,特别是在处理大型数据集或资源密集型任务时。
图片懒加载
1.使用intersectionObserver(交叉观察器)处理图片加载问题
相关链接:IntersectionObserver介绍
代码部分
html部分
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<img data-src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
<img data-src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
<img data-src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
<img data-src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
<img data-src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
<p>Lazy loading, also known as deferred loading or delay loading, serves several critical purposes in various
computing contexts. Here is an overview of its roles in different scenarios:In Web Design</p>
js部分
<script>
// 获取图片dom
let images = document.querySelectorAll('img');
// 创建intersectionObserver实例
const imagesObserver = new IntersectionObserver(entries => {
entries.forEach(entry => {
// 1.判断图片是否显示
if (entry.isIntersecting) {
// 2.获取原img的src内容
const image = entry.target;
const data_src = image.getAttribute('data-src');
// 3.设置其src属性
image.setAttribute('src', data_src)
// 4.取消观察
imagesObserver.unobserve(image);
}
})
})
// 观察所有图片
images.forEach(item => {
imagesObserver.observe(item);
})
</script>
封装的lazyload.js
// 1.获取图片集合
const images = document.querySelectorAll('img')
// 2.实例intersectionObserver
const imagesObserver = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const image = entry.target;
const data_src = image.getAttribute('data-src');
image.setAttribute('src', data_src);
imagesObserver.unobserve(image)
}
})
})
// 3.为图片创建实例
images.forEach(item => {
imagesObserver.observe(item)
})
2.使用img标签中的loading属性设置成为lazy,注意的时,该属性在一些浏览器中不兼容。
<img loading="lazy" src="https://q6.itc.cn/q_70/images03/20240126/4eaa7950e4214e8e83965f851318a03d.jpeg" alt="Image">
3.添加通过监听滚动事件进行处理
window.addEventListener('scroll', lazyLoadImages);
function lazyLoadImages() {
const images = document.querySelectorAll('img[data-src]');
images.forEach(image => {
if (image.getBoundingClientRect().top < window.innerHeight) {
image.src = image.getAttribute('data-src');
}
});
}