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

边栏推荐
- CorelDRAW 2022 Chinese Simplified 64 bit direct download
- Clickhouse 消除由group by产生的间隙
- go: finding module for package
- [graduation season · advanced technology Er] - summary from graduation to work
- 端口号和进程号的区别?
- 集群方法同步执行框架 Suona
- QML control type: tooltip
- 最新微信ipad协议 CODE获取 公众号授权等
- [multi source BFS] 934 Shortest Bridge
- AS400 entretien d'usine
猜你喜欢

How to maintain efficient collaboration in remote office and achieve stable growth of projects | community essay solicitation

我的PMP学习考试心得

How does the property send a text message to the owner?

The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer

Desai wisdom number - other charts (parallel coordinate chart): employment of fresh majors in 2021
![Pytoch -- foundation refers to the north_ II. What high school students can understand [back propagation and gradient descent]](/img/6e/279dbb7a8d7a5ecd240de464c5b8b2.png)
Pytoch -- foundation refers to the north_ II. What high school students can understand [back propagation and gradient descent]

Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry

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

计算特殊奖金

Int and bit group turn to each other
随机推荐
先写API文档还是先写代码?
How to realize the scene linkage of intelligent lock, lamp and intelligent curtain motor in zhiting?
What are the preferential activities for stock account opening? In addition, is it safe to open a mobile account?
AI 边缘计算平台 - BeagleBone AI 64 简介
Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]
SAP ALV汇总跟导出Excel 汇总数据不一致
如何学习和阅读代码
Template: globally balanced binary tree
Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
机器学习9-通用逼近器径向基函数神经网络,在新观点下审视PDA和SVM
halcon数组的一些使用
What is project management?
FL studio20.9 fruit software advanced Chinese edition electronic music arrangement
Short message sending solution in medical his industry
AS400 large factory interview
The whole process of AS400 API from zero to one
Short video platform development, relying on drawerlayout to achieve side sliding menu effect
P6773 [NOI2020] 命运(dp、线段树合并)
How does the property send a text message to the owner?
How do the top ten securities firms open accounts? Also, is it safe to open an account online?