当前位置:网站首页>图片懒加载的方案
图片懒加载的方案
2022-07-05 10:21:00 【码小龙.】
图片懒加载的方案
前言
图片懒加载是前端性能提升的重要方面之一,其主要的用途是: 减少网络加载,提升体验。
但为了影响体验,你需要最好写好布局样式,最好写好占位图。
懒加载的原理
其实图片的加载主要是因为图片的src引起的,而如果我们想要实现懒加载,就避免src路径的加载即可,我们可以将图片的路径存储到data-src的自定义数据属性上,然后在需要的时候按需加载。
按需加载的实现方式有两种:这里重点说第一种,目前在移动端以及在pc的瀑布流布局中都是第一种居多。
1 滚动到页面底部加载
2 分页加载
滚动到页面底部加载的原理
原来的方式:滚动监听
其主要实现的原理是对滚动事件的监听,当发现滚动条高度+滚动条高度等于document的高度时,就是达到页面的底部了,需要重新加载图片的时机。
KaTeX parse error: Expected '}', got 'EOF' at end of input: …ction(){ if((window).scrollTop() + ( w i n d o w ) . h e i g h t ( ) > = (window).height()>= (window).height()>=(document).height()){
var $div = ‘
- append添加的节点
- ’;
KaTeX parse error: Expected 'EOF', got '#' at position 3: ('#̲container').app…div)
}
});
新的方式:IntersectionObserver
我们去看下jq的lazyload的源码是如何写这部分函数的,核心是依赖IntersectionObserver,是检测元素是否进入视图,如果有进入,那么就会移除其lazy的class,其源码地址:链接
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
}
});
边栏推荐
- What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
- In wechat applet, after jumping from one page to another, I found that the page scrolled synchronously after returning
- Implementation of wechat applet bottom loading and pull-down refresh
- 微信核酸检测预约小程序系统毕业设计毕设(7)中期检查报告
- websocket
- “军备竞赛”时期的对比学习
- [paper reading] kgat: knowledge graph attention network for recommendation
- Learning note 4 -- Key Technologies of high-precision map (Part 2)
- 到底谁才是“良心”国产品牌?
- C语言实现QQ聊天室小项目 [完整源码]
猜你喜欢

双向RNN与堆叠的双向RNN

WorkManager學習一

Apple 5g chip research and development failure? It's too early to get rid of Qualcomm

2022年危险化学品生产单位安全生产管理人员特种作业证考试题库模拟考试平台操作

Secteur non technique, comment participer à devops?

What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?

Timed disappearance pop-up

The most complete is an I2C summary

Comparative learning in the period of "arms race"

Constraintlayout officially provides rounded imagefilterview
随机推荐
【tcp】服务器上tcp连接状态json形式输出
[dark horse morning post] Luo Yonghao responded to ridicule Oriental selection; Dong Qing's husband Mi Chunlei was executed for more than 700million; Geely officially acquired Meizu; Huawei releases M
pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
How to plan the career of a programmer?
想请教一下,十大券商有哪些?在线开户是安全么?
C语言实现QQ聊天室小项目 [完整源码]
Glide conclusion
[paper reading] kgat: knowledge graph attention network for recommendation
Nine degrees 1480: maximum ascending subsequence sum (dynamic programming idea for the maximum value)
小程序中自定义行内左滑按钮,类似于qq和wx消息界面那种
各位大佬,我测试起了3条线程同时往3个mysql表中写入,每条线程分别写入100000条数据,用了f
What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
How did automated specification inspection software develop?
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
Comparative learning in the period of "arms race"
微信核酸检测预约小程序系统毕业设计毕设(7)中期检查报告
uniapp
websocket
Secteur non technique, comment participer à devops?
请问大佬们 有遇到过flink cdc mongdb 执行flinksql 遇到这样的问题的么?