search
HomeWeb Front-endJS TutorialHow to use the jquery plug-in lazyload.js to delay loading images_jquery

If a webpage is very long and has many pictures, it will take a lot of time to download the pictures, which will affect the loading speed of the entire webpage. This lazy loading plug-in will load what you need to see based on your scrolling. Picture, and then it will request to download the picture from the background and finally display it. Through this plug-in, images can be downloaded only when they need to be displayed, thereby reducing server pressure and improving page loading speed.

Lazy Load plug-in principle

Modify the src attribute of the target img element to the orginal attribute to interrupt the loading of the image. Detect the scrolling state, then restore the src attribute of the img in the visible area of ​​the web page and then load the image, thus creating a buffered loading effect. Code introduction method:

Copy code The code is as follows:


> ;

But now, many Javascript experts have analyzed that this plug-in does not really play the role of slow loading. This is indeed the case, and official instructions and solutions have been given.

In fact, the reason is that in the new version of the browser, even if we delete the src attribute controlled by Javascript, the browser will still load the image.

So how should we solve it? In fact, it is very simple. You need to directly modify the structure of the HTML, add a new attribute to the img tag, point the value of the src attribute to the placeholder image, and add the data-original attribute to point to the real image address. For example:

Copy code The code is as follows:

How to use the jquery plug-in lazyload.js to delay loading images_jquery

Of course, in the above code we lazy load all the images in the page, but sometimes we don’t want this because some images don’t want them to be lazy loaded, so we can just do this:

For example, only buffer the images under loading class main

Copy code The code is as follows:

$(".main img").lazyload({
placeholder : "images/grey.gif",
effect : "fadeIn"
});

Load an image mounted with lazy class:

Copy code The code is as follows:

$("img.lazy").lazyload({
placeholder : "images/grey.gif",
effect : "fadeIn"
});

The rest can be deduced in the same way, just adjust the selector appropriately.

Advanced usage of lazyload.js:

The following part comes from the official document, which is a simple translation of the official document.

A more thoughtful approach

We have to think about this question. We have defined such a structure, so the source image will not be loaded in the web page. This source image will only be displayed when the Javascript is executed. If the user's browser does not support it or the user has turned off the option to support Javascript, then our image will not be displayed. In other words, without Javascript support, our images cannot be displayed.

To deal with this problem, we need to introduce the noscript tag. The general idea is as follows: use noscript to include the real image location, and when the browser does not support Javascript, display the image directly.

Copy code The code is as follows:

How to use the jquery plug-in lazyload.js to delay loading images_jquery
< ;noscript>How to use the jquery plug-in lazyload.js to delay loading images_jquery

For existing images, hide the processing and use the show() method to trigger the display.

Copy code The code is as follows:

.lazy {
display: none;
}

In this way, if the browser does not support Javascript, our customized img will not appear, but the image in noscript will be displayed. The specific implementation code is as follows:

Copy code The code is as follows:

$("img.lazy").show() .lazyload();

Load ahead

The default situation is that when you scroll to the image position, the plug-in starts loading. In this way, the user may first see a blank image, and then slowly appear. If you want to load this image in advance before the user scrolls, you can configure the parameters.

Copy code The code is as follows:

$("img.lazy").lazyload({
threshold : 200
});

threshold This parameter is used to load in advance. The meaning of the above statement is that when there are still 200 pixels away from the image, start loading the image.

Customized trigger events

The default trigger event is scrolling. When you scroll, it will be checked and loaded. You can use the event attribute to set your own loading event. Then you can customize the conditions that trigger this event and then load the image.

Copy code The code is as follows:

$("img.lazy").lazyload({
event: "click"
});

Customized display effects

The default image implementation effect is that there is no effect. After the download is completed, it will be displayed directly. This kind of user experience is not good. You can set the effect attribute to control the effect of displaying images. For example

Copy code The code is as follows:

$("img.lazy").lazyload({
effect: "fadeIn"
});

The effect of fadeIn is to change the transparency of the image and appear in a faded way.

Insert the image into a container

If you use a smartphone, you often go to application websites to download applications. They usually use a horizontal container to put some screenshots from your mobile phone. Using the container attribute, buffered loading can be easily implemented in the container. First, we need to define this container with css, and then load it with this plug-in.

Copy code The code is as follows:

#container { height: 600px; overflow: scroll; }
$("img.lazy").lazyload({
container: $("#container")
});

Load invisible image

Some images are invisible, so we add images with attributes such as display:none; to them. By default, this plugin will not load hidden invisible images. If we need to use it to load invisible images, we need to set skip_invisible to false, the code is as follows:

$("img.lazy").lazyload({ skip_invisible : false});
Okay, this is a brief introduction to the lazyload.js plug-in.
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
Java JPA 性能优化秘籍:让你的应用程序飞起来Java JPA 性能优化秘籍:让你的应用程序飞起来Feb 19, 2024 pm 09:03 PM

文章关键字:JavaJPA性能优化ORM实体管理JavaJPA(JavaPersistanceapi)是一种对象关系映射(ORM)框架,它允许你使用Java对象来操作数据库中的数据。JPA提供了与数据库交互的统一API,使得你可以使用同样的代码访问不同数据库。此外,JPA还支持懒加载、缓存和脏数据检测等特性,可以提高应用程序的性能。然而,如果使用不当,JPA性能可能会成为你应用程序的瓶颈。以下是一些常见的性能问题:N+1查询问题:当你在应用程序中使用JPQL查询时,可能遇到N+1查询问题。在这种

Linux 动态链接与静态链接原来是这么回事?Linux 动态链接与静态链接原来是这么回事?Feb 05, 2024 pm 05:45 PM

老规矩,先提出几个问题:为什么要进行动态链接?如何进行动态链接?什么是地址无关代码技术?什么是延迟绑定技术?如何在程序运行过程中进行显式链接?为什么要进行动态链接?动态链接的出现是为了解决静态链接的一些缺点:节约内存和磁盘空间:如下图所示,Program1和Program2分别包含Program1.o和Program2.o两个模块,他们都需要Lib.o模块。静态链接情况下,两个目标文件都用到Lib.o这个模块,所以它们同时在链接输出的可执行文件Program1和program2中有副本,同时运行

如何阻止iframe加载事件如何阻止iframe加载事件Feb 19, 2024 am 08:02 AM

如何防止iframe加载事件在网页开发中,我们常常会使用iframe标签来嵌入其他网页或内容。默认情况下,当浏览器加载iframe时,会触发加载事件。然而,在某些情况下,我们可能希望延迟加载iframe,或者完全阻止加载事件。在本文中,我们将探讨如何通过代码示例来实现这个目标。一、延迟加载iframe如果要延迟加载iframe,我们可以使用

Java JPA 开源项目推荐:为你的项目注入新的活力Java JPA 开源项目推荐:为你的项目注入新的活力Feb 20, 2024 am 09:09 AM

在Java编程领域,JPA(JavaPersistenceapi)作为一种流行的持久化框架,为开发者提供了对关系型数据库进行操作的便捷方式。通过使用JPA,开发者可以轻松地将Java对象持久化到数据库中,并从数据库中检索数据,从而极大地提高了应用程序的开发效率和维护性。本文精心挑选了10个高质量的JavaJPA开源项目,涵盖了各种不同的功能和应用场景,旨在为开发者提供更多的灵感和解决方案,助力打造更高效和可靠的应用程序。这些项目包括:SpringDataJPA:springDataJPA是Spr

懒加载延迟加载什么意思懒加载延迟加载什么意思Nov 20, 2023 pm 02:12 PM

懒加载是一种程序设计模式,指的是在需要时才加载数据,而不是在对象初始化或加载时就立即获取数据的策略,懒加载的目的是为了延迟数据的加载,以节省系统资源和提高性能。

PHP7中的生成器:如何高效地处理大量数据和延迟加载?PHP7中的生成器:如何高效地处理大量数据和延迟加载?Oct 27, 2023 pm 07:31 PM

PHP7中引入了生成器(Generator)这一概念,它提供了一种高效地处理大量数据和延迟加载的方法。本文将从概念和原理入手,结合具体代码示例,介绍PHP7中生成器的使用方法和优势。生成器是一种特殊的函数,它不是一次性地将所有数据返回,而是按需生成数据。当函数执行到yield语句时,会将当前生成的值返回,并且函数的状态会被保存。下一次调用生成器函数时,函数会

使用C# Lazy 实现延迟加载的方法使用C# Lazy 实现延迟加载的方法Feb 19, 2024 am 09:42 AM

C#如何使用Lazy实现懒加载,需要具体代码示例在软件开发中,懒加载(Lazyloading)是一种延迟加载的技术,它可以帮助我们提高程序的性能和资源利用效率。在C#中,我们可以使用Lazy类来实现懒加载的功能。本文将介绍Lazy类的基本概念以及如何使用它来实现懒加载,同时会提供具体的代码示例。首先,我们需要了解Lazy

洞悉 Hibernate 框架的知识体系,成为持久层开发的专家洞悉 Hibernate 框架的知识体系,成为持久层开发的专家Feb 19, 2024 pm 01:03 PM

一、Hibernate框架的概述Hibernate框架是一个开源的ORM(对象关系映射)框架,它提供了对Java对象和数据库之间的自动映射。这使得开发者可以在Java代码中直接操作Java对象,而无需关心底层的数据库表和列的细节。Hibernate会自动将Java对象映射到数据库表,并在Java对象和数据库表之间同步数据。Hibernate框架具有以下几个特点:简单易用:Hibernate提供了直观的api,使得开发者可以轻松地实现数据对象的持久化操作。高效:Hibernate框架使用了高效的缓

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

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.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

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