search
HomeWeb Front-endHTML TutorialShare a technical tutorial on lazy loading of front-end images

I originally planned to write a blog about the detailed technology of delayed loading of front-end images yesterday afternoon. Unexpectedly, there were some problems with the company's project in the afternoon, so I have been changing the code for debugging. I spent the whole day today I was running outside again, and it was already evening when I came back. I had just finished eating, and I wanted to make up for it quickly, so that many friends who don’t understand the specific implementation of this aspect can learn from the experience earlier.

Share a technical tutorial on lazy loading of front-end images

The user experience of the front-end page is crucial to a website. When we visit some websites with a large amount of pictures, we often have This feeling: The pictures displayed in the visible area of my computer screen are always unable to be refreshed in time. This results in some impatient users who are unwilling to wait any longer. Simply closing the website to look at other websites will result in the loss of users of this website. This is often the last thing a website wants to see. So for such a situation, developers continue to work hard to I quickly came up with a solution, which is to load the pictures in the visible area immediately, and let the pictures that are not in the visible area and need to be scrolled through the scroll bar to be displayed after the pictures are scrolled into the visible area. This is It is much better than loading all image resources at once, which results in slower image refresh. The user experience is much better.

So, how to implement the technology of lazy loading of images? Let’s give a detailed introduction:

First, define the picture as three columns, with a total of 5 rows. The specific code is as follows:

Share a technical tutorial on lazy loading of front-end images


Share a technical tutorial on lazy loading of front-end images

The bootstrap layout technology used in it (of course, this is not the point), please look at the src in the img tag. At first, we did not give it the specific resource path of the image, but defined an attribute x-src ourselves. The value of this attribute is the resource path of the image. This is true for each line of img. Next, when the page loads When , we use jquery (of course, you can also use native javascript code, I am just here to save time) to loop through each img and determine whether each image is within the current visible area, and if so, display the image. Otherwise, we will process it later. We need to know three data here:

Note: Because what I wrote is that the image will be loaded when half of the image enters the visible area of ​​the browser, so the first Three data, this depends on your personal needs. If your requirement is that the picture will be loaded as soon as it enters the visible area, you can directly ignore the third data!!!!

1: The browser can The height of the view area

2: The offset of the picture relative to the document (only the height offset is needed here)

3: The height of the picture element itself

If the image is first relative to the offset of the document + half the height of the image element itself

Share a technical tutorial on lazy loading of front-end images

The specific effect is as follows:

Share a technical tutorial on lazy loading of front-end images

You can use the console See, although we have 5 rows of images, each row has 3 columns, only the first column of images is loaded (the image resources will be loaded only if the image height exceeds half of the img), and the others are not loaded. This This will make the picture refresh effect appear quickly. Then, the user needs to see more pictures. At this time, the scroll bar needs to be scrolled down to refresh more pictures. At this time, in addition to the above three In addition to the data, you also need to know the current scrolling distance of the scroll bar, if:

The offset of the image to the document + half the height of the image element itself

Share a technical tutorial on lazy loading of front-end images

The specific effects are as follows:

Share a technical tutorial on lazy loading of front-end images

You can see in the console that as the scroll bar scrolls, the pictures loaded in have changed from the original three to the current six, and the scroll bar continues to move Scroll down and pictures will be continuously loaded, resulting in a better user experience.

This is the specific implementation of delayed loading of images. Do you think the image is cool? If you want to see the specific implementation effect for yourself.

【Related recommendations】

1. Free html online video tutorial

2. html development manual

3. php.cn original html5 video tutorial

The above is the detailed content of Share a technical tutorial on lazy loading of front-end images. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools