当前位置:网站首页>js实现图片懒加载的一个详细方案(引入即可使用)
js实现图片懒加载的一个详细方案(引入即可使用)
2022-06-29 06:41:00 【止水、】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>懒加载</title>
<style> img {
width: 600px; height: 450px; } </style>
<script src="./lazyLoad.js"></script>
</head>
<body>
<img src="" data-src="./imgs/css3.png">
<img src="" data-src="./imgs/css3.png">
<img src="" data-src="./imgs/css3.png">
<img src="" data-src="./imgs/css3.png">
<img src="" data-src="./imgs/css3.png">
<img src="" data-src="./imgs/css3.png">
<img src="" data-src="./imgs/css3.png">
<img src="" data-src="./imgs/css3.png">
</body>
</html>
lazyLoad.js
/** * 在使用一下懒加载的方式之前,图片要设置宽高,不设置宽高,会判断所有图片都在可视区域内 * 整个懒加载方案还需要一下几点的优化: * 1. 可视区域的的元素,目前是浏览器可视窗口,无法对可视区域进行判断 * 2. 事件监听一直存在,目前没有做销毁,考虑在图片全部加载完成后,销毁会导致后续增加的图片就无法触发懒加载 * 3. 对于元素的选中,虽然是通过标签名+属性值,但后续还可进行优化,从而获取更加精准的懒加载目标 */
window.onload = function() {
var imgList = []
var leftInterval = 100 * window.devicePixelRatio // 横向阈值
var heightInterval = 200 * window.devicePixelRatio // 纵向阈值
var throttleTime = 1000 // 节流时间
var debounceTime = 1000 // 防抖时间
var errorSrc = './imgs/wallhaven-1jxw53.jpg' // 图片加载失败的图片
// 判断图片是否出现出现在可视区域的首选方案
var observer = new IntersectionObserver(function(entries) {
entries.forEach(item => {
if (item.isIntersecting) {
// 只处理src=""和没有src属性的图片
if (item.target.src !== null) {
handleImgSrc(item.target)
observer.unobserve(item.target)
}
}
})
})
// 给图片添加src,并处理onerror事件
function handleImgSrc(dom) {
// 已经显示的图片不再处理
if (dom.getAttribute('src')) return
const dataSrc = dom.getAttribute('data-src')
if (dataSrc) {
dom.src = dataSrc
}
dom.onerror = () => {
dom.src = errorSrc
}
}
// 节流函数
function throttle(callback, delay) {
let timer = null
let _this = this
return function(...args) {
if (!timer) {
timer = setTimeout(() => {
callback.call(_this, args)
timer = null
}, delay)
}
}
}
// 防抖函数
function debounce(callback, delay) {
let timer = null
let _this = this
return function(...args) {
clearTimeout(timer)
timer = setTimeout(() => {
callback.call(_this, args)
}, delay)
}
}
// 判断图片是否出现再可是区域额兼容性写法
function isIn(dom) {
const clientWidth = window.innerWidth,
clienHeight = window.innerHeight,
bound = dom.getBoundingClientRect()
return (bound.top < clienHeight + heightInterval && bound.left < clientWidth + leftInterval)
}
// 懒加载
function lazyLoad() {
imgList.forEach(item => {
if (IntersectionObserver) {
observer.observe(item)
} else if (isIn(item)) {
handleImgSrc(item)
}
})
}
// 根据标签name和属性,获取相应的dom
function getElementsByTagNameAndAttribute(tag, attribute) {
const allTags = document.getElementsByTagName(tag)
return Array.from(allTags).filter(item => {
return item.getAttribute(attribute)
})
}
// 这里用一个变量记录,后期优化可以方便清除监听,
// 目前没有设置清楚监听,考虑第一轮图片加载完后,将监听清除后,后面第二轮的图片就无法实现懒加载的功能
var throttleLazy = throttle(lazyLoad, 1000)
var debounceLazy = debounce(lazyLoad, 1000)
function init() {
imgList = getElementsByTagNameAndAttribute('img', 'data-src')
lazyLoad()
window.addEventListener('scroll', throttleLazy, throttleTime)
window.addEventListener('resize', debounceLazy, debounceTime)
}
init()
}
边栏推荐
- Loop nesting: why can large loops inside and small loops outside improve the running efficiency of programs
- 查看tensorflow是否支持GPU,以及测试程序
- 数组知识点小结
- Schnuka: what is visual positioning system? How visual positioning system works
- AI与元宇宙擦出火花:人类失去的只有枷锁,获得的是全方面的解放
- Explanation of swing transformer theory
- 【工控老马】PLC六路抢答器系统设计详解
- ROS当中的仿真时间以及Bag包操作
- Matlab Simulink simulation and analysis of power grid sweep frequency
- cv2.cvtColor
猜你喜欢

Appium environment setup

tf.count_nonzero
[translation] swarmed out. Design methods for building modern applications

Blue Bridge Cup - minimum frame

Schnuka: 3D machine vision inspection system 3D vision inspection application industry

Vulnhub's dc7 target

【深度之眼吴恩达第四期作业班】多元线性回归Linear Regression with multiple variables总结

Compiling principle: the king's way

Software testing

Vulnhub's dc6 target
随机推荐
How to permanently set Mysql to utf8 encoding?
TF. Repeat and stack operations of slim
【工控老马】基于西门子S7-200PLC的跑马灯控制系统的设计方案详解
Vulnhub's DC8 target
Schnuka: 3D visual recognition system 3D visual inspection principle
VMware vcenter/ESXI系列漏洞总结
Cv:: mat and Base64 conversion (including picture compression and decompression)
KingbaseES 中select distinct on 语句
tf.compat.v1.assign
100 lectures on Excel advanced drawing skills (VI) - practical application cases of Gantt chart in project progress
matlab 多普勒效应产生振动信号和处理
[industrial control old horse] detailed explanation of the design scheme of the running lamp control system based on Siemens S7-200PLC
Swin Transformer理论讲解
Viewing application and installation of Hana database license
Summary of array knowledge points
道闸控制器通讯协议
Kyushu cloud helps Inner Mongolia's "counting from the east to the west" project to drive the smart new ecology of the surveying and mapping industry
Schnuka: 3D machine vision inspection system 3D vision inspection application industry
搭建jenkins环境并自动关联打包好的工程jar进行自动发布
[industrial control old horse] detailed explanation of design principle of pattern fountain based on PLC