How to implement lazy loading of images in document content to optimize display performance?

Calendar 81

Optimize website display performance: A practical guide to implementing lazy loading of images in Anqi CMS

In today's internet age, the loading speed of websites is crucial for user experience and search engine rankings.Large image files are often one of the main culprits that slow down website loading speed.Luckily, the Lazy Loading technology for images can effectively solve this problem.Images are only loaded when the user scrolls to their visible area, thus significantly improving the initial loading speed of the page, saving bandwidth, and improving the overall user experience.

The AnQi CMS is an efficient enterprise-level content management system that fully considers the needs of website performance optimization.It has good support for lazy loading of images, allowing users to easily implement this feature when publishing document content without complex secondary development.

What is lazy loading of images and why is it important?

In simple terms, lazy loading of images is 'on-demand loading'.When a user visits a web page with a large number of images, the traditional loading method loads all the images at once, regardless of whether these images are within the user's current viewport (visible area on the screen).This may cause the page to load slowly, especially on mobile devices, and users may feel impatient while waiting, even leaving directly.

The advantages of image lazy loading are reflected in several aspects:

  • Improve page loading speed:Only load images in the visible area of the page when it is first loaded, greatly reducing the initial loading content, allowing users to see the main content of the page faster.
  • Improve user experience:The page responds faster, reducing the white screen time, and users can browse content more smoothly.
  • Save bandwidth resources:Images that the user has not scrolled to or have never visited will not be loaded, thereby saving unnecessary bandwidth consumption for the server and the user.
  • Beneficial for SEO optimization:Search engines are increasingly emphasizing the loading speed and user experience of websites. Faster loading speeds help improve the ranking of websites in search results.

How does AnQi CMS support lazy loading of images?

AnQi CMS provides specific template tag attributes in document content (Content) output to cooperate with the implementation of image lazy loading. The core isarchiveDetailIn the tag, forContentAdd a fieldlazyProperty.

When we use in the templatelazy="data-src"After such settings, Anqi CMS will intelligently handle the document content in the<img>tags. It will keep the original of the imagesrcThe attribute (i.e., the actual address of the image) is converted or moved to a custom attribute, such asdata-src. This way, when the browser parses HTML, it will not immediately load these images because theirsrcattribute is empty or points to a placeholder.

To truly implement lazy loading of images, it is necessary to work with frontend JavaScript code to “activate” these images. This JavaScript code typically runs when the image enters the user's viewport anddata-srcReplace the real image address withsrcto trigger the image loading.

Hands-on practice: Enable lazy loading of images in document content.

To enable lazy loading of images on the document detail page of your Anqi CMS website, you need to modify the template file used to display the document content. This usually involvesarchiveDetail.

Assuming you are usingarchiveDetailTag to output the documentContentField, the template code may look like this:

{# 默认用法,自动获取当前页面文档 #}
<div>文档内容:{% archiveDetail with name="Content" %}</div>

To enable lazy loading of images, you need to add this taglazy="data-src"Properties. At the same time, since the content may contain HTML tags, we usually cooperate with|safeA filter to ensure that HTML code is parsed correctly rather than being escaped. The modified template code will be like this:

{# 启用图片懒加载的文档内容输出 #}
{% archiveDetail articleContent with name="Content" lazy="data-src" %}
{{articleContent|safe}}

Here:

  • articleContentIs the variable name you define for the content data you obtain, you can customize it as needed.
  • name="Content"Specify the document to be retrieved.Contentfield.
  • lazy="data-src"It is the key to lazy loading. It tells Anqi CMS to output content while<img>label'ssrcattribute is replaced withdata-src. You can change it according to the requirements of the frontend lazy loading JavaScript library you choose,data-srcFor other values, for exampledata-originaletc.
  • {{articleContent|safe}}Make sure to output the obtained HTML content as safe HTML, not plain text, so that the<img>tags can be correctly identified by the browser.

Choose the appropriate JavaScript lazy loading scheme

As mentioned before, the Anqi CMS'slazyThe property setting only completes the preparation of the HTML structure, and the actual lazy loading function needs to be implemented through JavaScript. There are many mature JavaScript lazy loading libraries to choose from, such aslazysizes.js/vanilla-lazyloadBased on, they are usuallyIntersection Observer APIImplementation, with good performance and wide compatibility.

Start withlazysizes.jsFor example, its use is very simple:

  1. Introduce a JavaScript library:tolazysizes.jsFile included in your template file (usuallybase.htmlor other common header or footer template). For example:
    
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lazysizes.min.js" async=""></script>
    
  2. (Optional) Add placeholder image:You can set a background color or a low-quality placeholder for lazy-loaded images in CSS to improve user experience and avoid blank spaces before the image loads.
  3. (Optional) Configure lazysizes:For more advanced features, you can consultlazysizes.jsthe official documentation for configuration.

Whenlazysizes.jsAfter being introduced and executed, it automatically detects all tags withdata-src(or the other attributes you specify) of<img>, and automatically moves their values todata-srcwhen they enter the user's viewportsrcAttribute, thus triggering image loading.

Further optimization: Combine with other image processing features of Anqi CMS.

In addition to lazy loading images, Anqi CMS also provides other practical image processing features that can be used with lazy loading to further improve website performance:

  • Enable WebP image format:In the AnQi CMS backend, you can choose to enable the WebP image format.WebP is a modern image format that is usually smaller than JPEG and PNG files while maintaining the same or better quality.After enabling, uploaded images will be automatically converted to WebP format, reducing image size.
  • Automatically compress large images and thumbnails:The Anqi CMS allows you to set automatic compression of large images to a specified width and provides various thumbnail processing methods (proportional scaling by the longest side, padding by the longest side, and cropping by the shortest side).Set these parameters reasonably to ensure that the image size on the website is moderate, reducing file size, and further speeding up loading speed.
  • Use CDN for acceleration:Combining lazy loading with CDN (Content Delivery Network) allows images to be loaded from the server closest to the user, further shortening the distance of image transmission, and achieving faster loading speeds.

By following these steps, you can effectively implement lazy loading of images on the Anqi CMS website and combine it with other optimization strategies to provide your users with a faster, smoother browsing experience.

Frequently Asked Questions (FAQ)

1. Why did I set it according to the guidelazy="data-src", but the image still loaded directly?

This is likely because you only configured the Anqi CMS outputdata-srcThe attribute is present, but no corresponding JavaScript lazy loading library has been introduced to “activate” these images.lazy="data-src"It only tells the browser where the image address isdata-srcin, but it will not automatically move it tosrc. You need to uselazysizes.jssuch a front-end JavaScript library to listen for when these images enter the viewport, and assigndata-srcthe value tosrc.

2. Does lazy loading of images affect the SEO of a website?

In the past, lazy loading of images could indeed have an impact on SEO, because search engine crawlers might not be able to capture those images that have not been loaded.However, with the advancement of technology, modern search engines (such as Google) have been able to execute JavaScript and simulate user scrolling behavior, thus they are usually able to normally crawl lazy-loaded images.srcIn the attribute), it will not have a negative impact on SEO.

3. Can images outside the document content also be lazy-loaded (such as in the sidebar, carousel)?

Yes, in principle, any image can be implemented lazy loading. It is the of AnQi CMS.lazy="data-src"The attribute mainly acts on thearchiveDetaildocument output by the tagContentField. For other areas of the image, such as images directly written in the template,<img>tags, carousel images, or sidebar ad images, you need to manually modify their HTML structure, tosrcattribute is replaced withdata-src(Or other attributes required by lazy loading libraries), and ensure that they can also be recognized and processed by the JavaScript lazy loading library you introduce.This usually means you need to check and modify the relevant template files.

Related articles

How to call and display the cover image, thumbnail, or group image of a specified document?

It is crucial to have visually appealing content when building and operating a website.AnQiCMS provides a flexible way to manage and display the images of documents (including articles, products, etc.), whether as an eye-catching cover, a clever thumbnail, or a colorful group photo.Understand how to correctly call these images, which can help you better design and optimize the user experience of the website.AnQiCMS provides several main image fields for different content types, which have clear uses in the template: * **Cover logo (Logo)**

2025-11-08

How to display the document title, publish time, and view count on the document detail page?

Manage website content in AnQi CMS and ensure its accurate and beautiful display on the front-end page, which is the core of daily operation work.For each document detail page, clearly presenting the title, publication time, and views of the document can not only effectively improve user experience, but is also an important link in the transmission of content information.The AnQi CMS provides powerful and flexible template tags, allowing us to easily meet these requirements. ### Locate the document detail page template To modify the display content of the document detail page, we first need to find the corresponding template file.In AnQi CMS template structure

2025-11-08

How to display mathematical formulas and flowcharts on AnQiCMS web pages?

Today, with the increasingly rich digital content, it often seems inadequate to convey complex information solely through text.Especially in fields such as science, technology, and education, mathematical formulas and flowcharts are the key to expressing rigorous logic and clear steps. 幸运的是,AnQiCMS provides a simple and powerful way to easily integrate and display these professional contents on your web page. This article will introduce in detail how to use the Markdown editor and third-party libraries on your AnQiCMS website to achieve perfect presentation of mathematical formulas and flowcharts.

2025-11-08

How to correctly render Markdown editor content on the frontend page in HTML?

## AQS CMS Markdown content: How to perfectly present on the front-end page?In content creation, Markdown, with its concise and efficient syntax, has been favored by a wide range of content creators.AnQi CMS knows this and therefore provides strong Markdown editor support.But how can you ensure that the Markdown content you edit in the background is rendered correctly and beautifully on the website's frontend page?Behind this involve several key links, understanding them can help you manage the website content more skillfully.

2025-11-08

How to get and display the name and link of the category to which the current document belongs?

On the content detail page of a website, we often hope to clearly display the classification information of the current document, such as the name of the classification and the link to that classification.This not only helps visitors better understand the content structure, but also improves the website's navigation and user experience.Safe CMS provides us with a variety of flexible ways to meet this requirement. We will discuss several methods for obtaining and displaying the category name and link of the current document in the AnQiCMS template, and you can choose the most suitable solution according to your actual template design and requirements.### Method One

2025-11-08

How to display a list of recommended documents related to the current document on the document detail page?

In website content operations, providing users with relevant recommended content not only effectively enhances the depth and duration of page browsing, but also optimizes user experience, indirectly assisting in the SEO performance.In AnQiCMS (AnQi Content Management System), implementing this feature is flexible and direct.Next, we will discuss how to seamlessly integrate and display the recommended document list closely related to the current document on the document detail page. ### Core Value: Why should recommended documents be displayed on the document detail page?

2025-11-08

How to display the titles and links of the previous and next documents on the document detail page?

In website content operation, it is crucial to provide readers with a smooth reading experience.If a user finishes reading a document and can easily navigate to the previous or next related content, it not only effectively extends the user's stay on the website and increases page views, but also helps search engines better understand the structure and content relevance of the website, thereby having a positive impact on SEO.AnQiCMS (AnQiCMS) has fully considered this point in the design of template functions, providing simple and powerful tags, making it easy to display the title and link of the previous and next documents on the document detail page

2025-11-08

How to call and display the data of the custom model field in the document?

In Anqi CMS, the flexible content model is one of its core strengths, allowing us to create various information carriers such as articles, products, and more according to different business needs and define unique properties for them.How to present the data of these customized fields in the website front-end after we have set them for the document is a major concern for many users.This article will provide a detailed explanation of this process, helping you easily master the custom model fields of Anqi CMS. ### Understanding the Value of Custom Model Fields Firstly, let's clarify the importance of custom model fields.

2025-11-08