Are you still worried about the loading speed of web pages? Have you ever found that web pages are very fast in some browsers, but very slow in others? Today I would like to introduce to you why these situations occur, and share with you the specific implementation methods
1. First explain to you why you need to delay loading of images
Everyone knows the common formats of images Well, a picture is a group of pixels that are put together little by little. The more pixels a picture has, the larger it will be. Generally, pictures are several kb, tens of kb, or several MB in size. Imagine that if your image is 10kb, then it doesn't matter whether it is delayed or not, but if it is tens of kb or hundreds of kb, how many more seconds will the user be asked to wait? You can’t make users wait for tens of seconds to watch the content just because of a picture, right?
2. Implementation of delayed image loading
In fact, the function of delayed image loading is not uncommon, and some browsers have implemented browser delayed loading of images, but it is not supported. What about the browser?
Then I’ll figure it out myself, okay! Without further ado, let’s paste the code first:
// window.imageList=[ // { id:"图像1ID", url:"URL", onload=加载函数 }, // { id:"图像2ID", url:"URL", onload=加载函数 }, // { id:"图像3ID", url:"URL", onload=加载函数 }, // .... // ]; //在页面加载完成后调用 onLoadImage(); function onLoadImage() { var i=0; if(window.imageList){ //if(typeof window.imageList != "array") // return; for(i=0;i<window.imageList.length;i++){ var now=window.imageList[i]; var id,url,onloadfn; if(now.id){ id=now.id; } if(now.url){ url=now.url; } if(now.onload){ onloadfn=now.onload; } if(id == null || url == null){ continue; } var nelement = document.getElementById(id); nelement.src = url; if(onloadfn) (onloadfn)(nelement,url); id = url = onloadfn = null; } } console.log("All Image is loaded!Total "+i+" Image!"); if(window.onImageAllLoad) (window.onImageAllLoad)(); }
Example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>这是一个测试</title> </head> <body> <img src="/static/imghwm/default1.png" data-src="SuperImageLoader.js" class="lazy" id="img1" alt="正在加载"></img> <img src="/static/imghwm/default1.png" data-src="SuperImageLoader.js" class="lazy" id="img2" alt="正在加载"></img> <img src="/static/imghwm/default1.png" data-src="SuperImageLoader.js" class="lazy" id="img3" alt="正在加载"></img> <script ></script> <script> window.imageList=[ { id:"img1", url:"https://www.baidu.com/img/baidu_jgylogo3.gif", onload:function(){ alert('第一张图片已加载完成!'); } }, { id:"img2", url:"https://www.baidu.com/img/baidu_jgylogo3.gif"}, { id:"img3", url:"https://www.baidu.com/img/baidu_jgylogo3.gif"} ]; window.onImageAllLoad=function(){ alert('所有图片已加载!'); } onLoadImage(); </script> </body> </html>
The following is a test screenshot:
The above is the detailed content of How to delay loading of images?. For more information, please follow other related articles on the PHP Chinese website!

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1
Easy-to-use and free code editor
