This article mainly introduces in detail the use of imgLazyLoading.js for lazy loading of images. It has certain reference value. Interested friends can refer to it.
This article mainly introduces the use of images on the web front-end. Lazy loading imgLazyLoading, for your reference, the specific content is as follows
1, html code
//懒加载对象目标代码 <img src="/static/imghwm/default1.png" data-src="__PUBLIC__/images/home/icon_pingtuan.png" class="lazy" original alt="Image lazy loading imgLazyLoading.js" > //引用本地js <script src="__PUBLIC__/js/imgLazyLoading.min.js"></script> <script src="__PUBLIC__/js/imgLazyLoading.js"></script>
2, js code
imgLazyLoading.min.js
jQuery.fn.extend({ delayLoading: function (a) { function g(d) { var b, c; if (a.container === undefined || a.container === window) { b = $(window).scrollTop(); c = $(window).height() + $(window).scrollTop() } else { b = $(a.container).offset().top; c = $(a.container).offset().top + $(a.container).height() } return d.offset().top + d.height() + a.beforehand >= b && c >= d.offset().top - a.beforehand } function h(d) { var b, c; if (a.container === undefined || a.container === window) { b = $(window).scrollLeft(); c = $(window).width() + $(window).scrollLeft() } else { b = $(a.container).offset().left; c = $(a.container).offset().left + $(a.container).width() } return d.offset().left + d.width() + a.beforehand >= b && c >= d.offset().left - a.beforehand } function f() { e.filter("img[" + a.imgSrcAttr + "]").each(function (d, b) { if ($(b).attr(a.imgSrcAttr) !== undefined && $(b).attr(a.imgSrcAttr) !== null && $(b).attr(a.imgSrcAttr) !== "" && g($(b)) && h($(b))) { var c = new Image; c.onload = function () { $(b).attr("src", c.src); a.duration !== 0 && $(b).hide().fadeIn(a.duration); $(b).removeAttr(a.imgSrcAttr); a.success($(b)) }; c.onerror = function () { $(b).attr("src", a.errorImg); $(b).removeAttr(a.imgSrcAttr); a.error($(b)) }; c.src = $(b).attr(a.imgSrcAttr) } }) } a = jQuery.extend({ defaultImg: "", errorImg: "", imgSrcAttr: "originalSrc", beforehand: 0, event: "scroll", duration: "normal", container: window, success: function () { }, error: function () { } }, a || {}); if (a.errorImg === undefined || a.errorImg === null || a.errorImg === "")a.errorImg = a.defaultImg; var e = $(this); if (e.attr("src") === undefined || e.attr("src") === null || e.attr("src") === "")e.attr("src", a.defaultImg); f(); $(a.container).bind(a.event, function () { f() }) } });
imgLazyLoading.js
$(function () { $("img").delayLoading({ //defaultImg: "__PUBLIC__/images/img/loading.gif", // 预加载前显示的图片 errorImg: "", // 读取图片错误时替换图片(默认:与defaultImg一样) imgSrcAttr: "originalSrc", // 记录图片路径的属性(默认:originalSrc,页面img的src属性也要替换为originalSrc) beforehand: 0, // 预先提前多少像素加载图片(默认:0) event: "scroll", // 触发加载图片事件(默认:scroll) duration: "normal", // 三种预定淡出(入)速度之一的字符串("slow", "normal", or "fast")或表示动画时长的毫秒数值(如:1000),默认:"normal" container: window, // 对象加载的位置容器(默认:window) success: function (imgObj) { }, // 加载图片成功后的回调函数(默认:不执行任何操作) error: function (imgObj) { } // 加载图片失败后的回调函数(默认:不执行任何操作) }); });
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
vue-cli configuration file (detailed tutorial)
Use jQuery to encapsulate animate.css (detailed tutorial)
How to reset the idle state in vuex
The above is the detailed content of Image lazy loading imgLazyLoading.js. For more information, please follow other related articles on the PHP Chinese website!

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.


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

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),

Dreamweaver CS6
Visual web development tools

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
