当前位置:网站首页>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 :

边栏推荐
- In the fourth week of June, the list - flying melon data up main growth ranking list (BiliBili platform) was released!
- [2022] Jiangxi postgraduate mathematical modeling scheme and code
- Comment réaliser la liaison entre la serrure intelligente et la lampe, la scène du moteur de rideau intelligent dans le timing intelligent?
- PMP是什麼?
- Go import self built package
- Do you write API documents or code first?
- 开源基础软件公司,寻找一起创造未来的你(API7.ai)
- Ernie gram, an explicit and complete n-gram mask language model, implements explicit n-gram semantic unit knowledge modeling.
- 聚焦绿色低碳,数据中心散热进入“智能冷却”新时代
- 522. Longest special sequence II
猜你喜欢

Fix names in the table (first character uppercase, other lowercase)

如何在智汀中实现智能锁与灯、智能窗帘电机场景联动?
![Pytorch —— 基础指北_贰 高中生都能看懂的[反向传播和梯度下降]](/img/6e/279dbb7a8d7a5ecd240de464c5b8b2.png)
Pytorch —— 基础指北_贰 高中生都能看懂的[反向传播和梯度下降]

聚焦绿色低碳,数据中心散热进入“智能冷却”新时代

(translation) use eyebrow shaped text to improve Title click through rate

Desai wisdom number - other charts (parallel coordinate chart): employment of fresh majors in 2021

修复表中的名字(首字符大写,其他小写)

(翻译)实时内联验证更容易让用户犯错的原因

【2022年】江西省研究生数学建模方案、代码

机器学习10-信念贝叶斯分类器
随机推荐
What is project management?
FL studio20.9 fruit software advanced Chinese edition electronic music arrangement
Pytoch -- foundation refers to the north_ II. What high school students can understand [back propagation and gradient descent]
Upstream and downstream in software development
C#生成putty格式的ppk文件(支持passphrase)
端口号和进程号的区别?
nacos配置中心使用教程
What are the preferential activities for stock account opening? In addition, is it safe to open a mobile account?
开源基础软件公司,寻找一起创造未来的你(API7.ai)
Alphabet-Rearrange-Inator 3000(字典树自定义排序)
如何在智汀中實現智能鎖與燈、智能窗簾電機場景聯動?
如何学习和阅读代码
Pulsar Geo Replication/灾备/地域复制
A preliminary understanding of operator overloading
ANR问题的分析与解决思路
The latest CSDN salary increase technology stack in 2022 overview of APP automated testing
What are the applications of SMS in enterprises?
聚焦绿色低碳,数据中心散热进入“智能冷却”新时代
AS400 大廠面試
Short message sending solution in medical his industry