当前位置:网站首页>Image preloading in JS
Image preloading in JS
2022-07-01 02:23:00 【Front end tripod】
Picture preload
Scene description
We often have pictures loaded in the page during development , When the image to be loaded is too large or the network is not good , A blank page will appear at the image position, which will greatly affect the user experience .
Another thing is that I want to load the pictures in other pages that I will jump to in advance , Prevent images from affecting the loading of the page . Such scenarios can use image preloading technology to optimize our user experience .
Realization principle
The principle of image preloading is to use the browser to cache the same image address , Load pictures locally in advance , Wait until you use it to read pictures from the cache .
Implementation steps
The image preloading implementation usually uses a smaller loading Image space occupying , Then use the asynchronous method to load the pictures , When the image is loaded, fill it in img Node .
Concrete realization
const imgSet = (function() {
const imgNode = document.createElement('img');
document.body.appendChild(imgNode);
const img = new Image();
// The picture is added to the display after loading img Node
img.onload = function() {
imgNode.src = img.src;
}
return (url) => {
// to img Node add loading Occupation map
imgNode.src = './images/ef9081ecc65482ed7bcfc9b7e0b548d6.gif';
// Load pictures to use
img.src = url;
}
})();
imgSet('https://img1.baidu.com/it/u=3615107494,579574018&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500');
Effect comparison
First, let's take a look at the effect of directly loading pictures :

Then look at the effect of using image preloading :

边栏推荐
- Machine learning 10 belief Bayesian classifier
- [fundamentals of wireless communication-15]: illustrated mobile communication technology and application development-3-overview of digital communication 2G GSM, CDMA, 3G wdcma/cdma200/td-scdma, 4G LTE
- VirtualBox installation enhancements
- SWT/ANR问题--StorageManagerService卡住
- 项目管理是什么?
- How to learn and read code
- 5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?
- 视觉特效,图片转成漫画功能
- With one-stop insight into industry hot spots, the new function "traffic market" of feigua data station B is launched!
- 对象与对象变量
猜你喜欢
![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]

pycharm 软件deployment 灰色 无法点

There is no future to be expected. It is just the last fantasy of a migrant worker before he dies

最新微信ipad协议 CODE获取 公众号授权等

如何在智汀中实现智能锁与灯、智能窗帘电机场景联动?

With one-stop insight into industry hot spots, the new function "traffic market" of feigua data station B is launched!

Find the length of the common part of two line segments

(翻译)实时内联验证更容易让用户犯错的原因
![SQL语句关联表 如何添加关联表的条件 [需要null值或不需要null值]](/img/91/0efbc13597be4dba5b9cf4e8644e35.png)
SQL语句关联表 如何添加关联表的条件 [需要null值或不需要null值]

删除重复的电子邮箱
随机推荐
The latest CSDN salary increase technology stack in 2022 overview of APP automated testing
P6773 [NOI2020] 命运(dp、线段树合并)
What other hot spots are hidden under 1500W playback? Station B 2 future trends you can't miss
Machine learning 9-universal approximator radial basis function neural network, examining PDA and SVM from a new perspective
Windows quick add boot entry
(翻译)使用眉状文本提高标题点击率
计算特殊奖金
【毕业季·进击的技术er】--毕业到工作小结
Electron pit Addon
【2022年】江西省研究生数学建模方案、代码
Video tutorial | Chang'an chain launched a series of video tutorial collections (Introduction)
Qu'est - ce que le PMP?
运算符重载的初识
SWT/ANR问题--Binder Stuck
nacos配置中心使用教程
RocketQA:通过跨批次负采样(cross-batch negatives)、去噪的强负例采样(denoised hard negative sampling)与数据增强(data augment
如何在智汀中实现智能锁与灯、智能窗帘电机场景联动?
[proteus simulation] Arduino UNO +74c922 keyboard decoding drive 4x4 matrix keyboard
Objects and object variables
With one-stop insight into industry hot spots, the new function "traffic market" of feigua data station B is launched!