当前位置:网站首页>js中的图片预加载
js中的图片预加载
2022-07-01 02:21:00 【前端三脚猫】
图片预加载
场景描述
我们在开发中经常会有页面中会加载图片的情况,当要加载的图片过大或者网络不太好的时候,图片位置就会出现空白页面非常影响用户体验。
再有就是我想把将要跳转的其他页面里的图片提前加载好,防止因为图片影响页面的加载。诸如此类的场景都可以使用图片预加载技术来对我们的用户体验进行优化。
实现原理
图片预加载的原理就是利用浏览器对同一图片地址的缓存,将图片提前加载到本地,等到使用的时候就从缓存中读取图片。
实现步骤
图片预加载实现一般先用一张较小的loading图片进行占位,然后使用异步方式进行图片加载,等图片加载好了再把它填充到 img 节点里即可。
具体实现
const imgSet = (function() {
const imgNode = document.createElement('img');
document.body.appendChild(imgNode);
const img = new Image();
// 图片加载好后添加到展示的img节点上
img.onload = function() {
imgNode.src = img.src;
}
return (url) => {
// 给img节点添加loading占位图
imgNode.src = './images/ef9081ecc65482ed7bcfc9b7e0b548d6.gif';
// 加载将要使用的图片
img.src = url;
}
})();
imgSet('https://img1.baidu.com/it/u=3615107494,579574018&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500');
效果对比
首先看下直接加载图片的效果:

然后在看下使用图片预加载的效果:

边栏推荐
- AS400 API 从零到一的整个历程
- FL studio20.9 fruit software advanced Chinese edition electronic music arrangement
- Fix names in the table (first character uppercase, other lowercase)
- CentOS installs multiple versions of PHP and switches
- Clickhouse eliminates the gap caused by group by
- Comment réaliser la liaison entre la serrure intelligente et la lampe, la scène du moteur de rideau intelligent dans le timing intelligent?
- I want to know how to open a stock account? Is it safe to open an account online?
- Some uses of Halcon array
- Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
- Pytorch - - Basic Reference North Deux élèves du secondaire peuvent comprendre [Rétropropagation et Gradient descendant]
猜你喜欢
![How to add a condition for an associated table in an SQL statement [null value required or not required]](/img/91/0efbc13597be4dba5b9cf4e8644e35.png)
How to add a condition for an associated table in an SQL statement [null value required or not required]

How to realize the scene linkage of intelligent lock, lamp and intelligent curtain motor in zhiting?

求两个线段公共部分的长度

QML控件类型:ToolTip

LabVIEW calculates the camera image sensor resolution and lens focal length

What is project management?

详解数据治理知识体系

QML control type: tooltip

Fast understanding of forward proxy and reverse proxy

Upstream and downstream in software development
随机推荐
SWT / anr issues - ams/wms
The whole process of AS400 API from zero to one
Check the disk usage of MySQL database
[multi source BFS] 934 Shortest Bridge
运算符重载的初识
静态域与静态方法
Go import self built package
QT web development - VIDEO - Notes
522. 最长的特殊序列 II
Small program cloud development -- wechat official account article collection
[punch in questions] integrated daily 5 questions sharing (phase I)
centos 安装多个版本的php并切换
Upstream and downstream in software development
SQL语句关联表 如何添加关联表的条件 [需要null值或不需要null值]
Open source basic software companies, looking for you to create the future together (api7.ai)
SWT / anr problem - anr/je causes SWT
Laravel event & Monitoring
House change for agricultural products? "Disguised" house purchase subsidy!
Objects and object variables
小程序云开发之--微信公众号文章采集篇